recvfrom(UDP)-error:Resource temporarily unavailable

Hi all,
I’m debuging the UDP communcation function, based on the socket.But the function(recvfrom) always returns the error (errno = 11,Resource temporarily unavailable) .The following is my code:
//
tv.tv_sec = 1;
tv.tv_usec = 0;
sockAddrSize = sizeof(struct sockaddr_in);
bzero((char ) &clientAddr, sockAddrSize);
clientAddr.sin_len = sockAddrSize;
clientAddr.sin_family = AF_INET;
clientAddr.sin_port = htons(5000);
clientAddr.sin_addr.s_addr = inet_addr(masterIP);//htonl(INADDR_ANY);
/
create a UDP-based socket /
if ((cFd = socket(AF_INET, SOCK_DGRAM, 0)) == 0) {
printf(“socket error\n”);
return -1;
}
/
init serverAddr */
bzero(&serverAddr, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_len = sizeof(serverAddr);
serverAddr.sin_addr.s_addr = inet_addr(slaveIP);//htonl(INADDR_ANY); //
serverAddr.sin_port = htons(5000);

//set timeout
setsockopt(cFd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
//
for (;:wink: {

   //printf("Send ......\n");
   errno = EOK;
   ///* send buffer */
   ret = sendto( cFd,  buf,buflen,0,(struct sockaddr *) &serverAddr,sizeof(struct sockaddr));      
  if(ret == -1)
   {
printf("error : %d  ", errno);
printf("That means: %s  ", strerror(errno));
printf("Send fail\n");
  }

 errno = EOK;
 //printf("receiving ......\n");
 ret = recvfrom(cFd,recvbuff,200,0,(struct sockaddr *) &fromAddr,(socklen_t*)&sockAddrSize);      
 if (ret == -1) {
printf("error : %d  ",  errno);
printf("That means: %s", strerror(errno));
     } else {
           //handle the data;
           ......
     }

}
on the implemention, I set the SO_RCVTIMEO parameter. the recvfrom function return -1.
what’s wrong with the code? thanks!

Regards,
mark

I would remove the set_socket (…,SO_RCVTIMEO,…) ; and see if the same thing happens. If not, then you know the problem is related to the timeout you are requesting. If it still fails, then you know something else is wrong.

The host program will be blocked in the recvform() function when I remove the SO_RCVTIMEO parameter.But, by using wireshark, I can get the data frame be send by my host program and the data frame responsed by the echo server. After that, the host program will send a ICMP package to the echo sever.

Mark,

I assume you know you can set the socket to be non-blocking. Then if there is no message it returns right away and you can handle that any way you like.

The other option is to use a select() call with a timeout and pass in the socket descriptor to select(). That way you can wait for data or a specified interval.

Tim

You missed my point. I wasn’t saying that you should change your code permanently. I was saying change it to isolate where the problem is.