I am currently writing a small UDP server program on Linux. The UDP server will receive packets from two different peers and will perform different operations based on which it received the packet. I am trying to determine the source where I get the package from. However, when select returns and recvfrom is called, it returns with an invalid argument error. If I pass NULL as the last second arguments, recvfrom will succeed.
I tried to declare fromAddr as struct sockaddr_storage , struct sockaddr_in , struct sockaddr without any success. Is there something wrong with this code? Is it correct to determine the source of the package?
Below is a snippet of code.
` if((pkInfo->rcvLen=recvfrom(psInfo->sockFd, pkInfo->buffer, MAX_PKTSZ, 0, NULL, &(addrLen) )) < 0) { perror("RecvFrom failed\n"); } else { #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); } `
linux sockets
Aditya sehgal
source share