How to make a blocking recv() call to be timeout ?

Situation: If process control is at recv() (which is bloking) always and there is a need to go for other timely actions.

solution : usage of MSG_DONTWAIT flag in recv() call


Other solutions : threads or signaling solutions but that makes process time longer and more complicated.

Even in blocking sockets for recv() call , we can make timeout using MSG_DONTWAIT flag and this is a default(can be configure) timeout value set in a file /usr/include/sys/socket.h as #define MSG_DONTWAIT 0x80, which counts 128 sec(Solaris 10 Specific).


maxToRead = recv(source_fd,msgLen, 10, MSG_PEEK | MSG_DONTWAIT);
if(maxToRead > 0)
readn = recv(source_fd, buffer, msgLen, MSG_DONTWAIT);

When setting the MSG_PEEK flag , the data is not removed from the reception buffer so that the next read returns the same data.

Here msgLen is what first recv() call receives as data, probably data length to be extracted in second recv call.

No comments :

Post a Comment