Saturday, January 8, 2011

When Socket Recv() call got blocked even sockfd is having data

Problem Description:

We have a server where in it is waiting to receive some data so that it can respond with answer. And we have coded such that we are blocked on Select() call,
Once select() gets unblocked we will verity on which FD the data has been triggered using FD_ISSET ,

But the problem is FD_ISSET is returning success mentioning there is data on that socket descriptor, but when we tried to call recv() the data is not getting read so on recv() call the program got blocked.

Don’t know the reason why this happens: (Read like etherenet will do some kind of filling the multiple messages for single eth packet, but not sure whether this is the reason)

But I solved this issue :

By making recv call socket as unblocking
And
Here is how:

(In server code : accept() will return the socket (hSok) then make that socket descriptor as unblocking

int fcntl_flag = 0;

if ((fcntl_flag = fcntl(hSok, F_GETFL, 0)) < 0)
{
printf("\n Error getting sockfd flags F_GETFL %s\n",strerror(errno));

}
if (fcntl(hSok, F_SETFL, fcntl_flag | O_NONBLOCK) < 0)
{
printf("\n Error setting sockfd flags F_SETFL %s\n",strerror(errno));
}

No comments:

Post a Comment