Rehab Reda
Rehab Reda

Reputation: 193

Windows TCP Socket Timeout C++

I have a question

How to know if a client didn't respond to the server during a specified time ?!

I am using threading not select function.

your help will be greatly appreciated :)

Thank you.

Upvotes: 1

Views: 2907

Answers (1)

László Papp
László Papp

Reputation: 53155

You would need to look into [setting the socket option][1] as follows:

setsockopt(sockid, SOL_SOCKET, SO_RCVTIMEO,(char *)&tv,sizeof(struct timeval));

Having that done, you can check againt SOCKET_ERROR when calling the receive and/or send functions. The specific error code can be obtained by calling WSAGetLastError.

This is one of those potential error codes:

WSAEWOULDBLOCK 10035

Resource temporarily unavailable.

This error is returned from operations on nonblocking sockets that cannot be completed immediately, for example recv when no data is queued to be read from the socket. It is a nonfatal error, and the operation should be retried later. It is normal for WSAEWOULDBLOCK to be reported as the result from calling connect on a nonblocking SOCK_STREAM socket, since some time must elapse for the connection to be established.

Upvotes: 1

Related Questions