recvfrom returns invalid argument when *from* is passed

Posted by Aditya Sehgal on Stack Overflow See other posts from Stack Overflow or by Aditya Sehgal
Published on 2010-06-08T17:19:45Z Indexed on 2010/06/08 17:22 UTC
Read the original article Hit count: 210

I am currently writing a small UDP server program in linux. The UDP server will receive packets from two different peers and will perform different operations based on from which peer it received the packet. I am trying to determine the source from where I receive the packet. However, when select returns and recvfrom is called, it returns with an error of Invalid Argument. If I pass NULL as the second last arguments, recvfrom succeeds.

I have tried declaring fromAddr as struct sockaddr_storage, struct sockaddr_in, struct sockaddr without any success. Is their something wrong with this code? Is this the correct way to determine the source of the packet?

The code snippet follows.

`           
    /*TODO : update for TCP. use recv */
    if((pkInfo->rcvLen=recvfrom(psInfo->sockFd,
                                pkInfo->buffer,
                                MAX_PKTSZ,
                                0,
                               /* (struct sockaddr*)&fromAddr,*/
                               NULL,
                              &(addrLen)
                              )) < 0)
    {
       perror("RecvFrom failed\n");
    }
    else
    {
       /*Apply Filter */
    #if 0
       struct sockaddr_in* tmpAddr;
       tmpAddr = (struct sockaddr_in* )&fromAddr;
       printf("Received Msg From %s\n",inet_ntoa(tmpAddr->sin_addr));
    #endif
      printf("Packet Received of len = %d\n",pkInfo->rcvLen);
    }

`

© Stack Overflow or respective owner

Related posts about linux

Related posts about sockets