tags:

views:

133

answers:

1

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);
    }

`
+1  A: 

You must initialise addrLen to sizeof fromAddr before calling recvfrom() - it is an input/output parameter. Using struct sockaddr_storage to declare fromAddr is correct.

caf
Thanks that was it. Silly me.
Aditya Sehgal