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 (; {
//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