1

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.

Rehab Reda
  • 193
  • 7
  • 16

1 Answers1

1

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.

Community
  • 1
  • 1
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • Thank you so much ? if i understood you answer it is just for setting timeout value at the socket but what happens if a timeout occurs will it calls a function ? .. I want to be notified at the server that a time out has occurred – Rehab Reda Apr 06 '14 at 09:48
  • @RehabReda: you will be notified by the return value. – László Papp Apr 06 '14 at 09:49
  • the return value from this function ? recv(clientSocket, receivedData, BUFFER_SIZE, 0); what will it be ? and really thank you :) – Rehab Reda Apr 06 '14 at 09:51
  • @RehabReda You're expected to have read the Winsock documentation, which clearly states that recv() return SOCKET_ERROR on error, with a side-effect in the value returned by WSAGetLastError(). – user207421 Apr 06 '14 at 12:02
  • @EJP: strictly speaking, not -1, but SOCKET_ERROR. – László Papp Apr 06 '14 at 12:04
  • @LazloPapp You don't have any evidence about who downvoted the answer. If you are trying to thank me for the correction, or even acknowledge it, you're going a strange way about it. – user207421 Apr 06 '14 at 12:07
  • @EJP: I cannot acknowledge what you wrote because it does not seem to be true unfortunately. It is not EAGAIN and EWOULDBLOCK, but WSAEWOULDBLOCK. I acknowledge the WSAEIMTDOUT part, thanks. That was correct an observation. – László Papp Apr 06 '14 at 12:08