-
August 4th, 2012, 10:53 AM
#1
Interpreting return code of recv() socket api
Hi.
I have a situation where client waits on recv() on a socket in one thread. And , I am monitoring the same socket for timeout on another thread. When the timeout occurs , the second thread drops the socket.
As a result the first thread waiting on recv() gets a return code 0 (no bytes received ) with no error number set.
Now there is another case where recv() at client returns with 0 before the timeout , which is , when there is an issue with the server and it terminates the connection without sending any data.
Is the above behaviour of recv() in both cases correct ? If yes , is there a way at the client to differentiate between the two cases ? In both cases recv() returns with 0 and no error number.
Please let me know.
-
August 4th, 2012, 12:54 PM
#2
Re: Interpreting return code of recv() socket api
From MSDN:
Return Values
If no error occurs, recv returns the number of bytes received. If the connection has been gracefully closed, the return value is zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.
Victor Nijegorodov
-
August 6th, 2012, 01:30 AM
#3
Re: Interpreting return code of recv() socket api
Thanks for the reply victor.
The error number is set when recv() returns a -1 which can be retrieved by WSAGetLastError.
However , in my case I am getting a 0 from recv() in two cases in which no error number is set.
I am looking for a way to differentiate between these two cases when return code of recv is 0 and no error number is set.
-
August 6th, 2012, 02:31 AM
#4
Re: Interpreting return code of recv() socket api
Originally Posted by yaniv135
However , in my case I am getting a 0 from recv() in two cases in which no error number is set.
According to docs the return value zero is usually caused by gracefully closed connection...
Victor Nijegorodov
-
August 6th, 2012, 02:38 AM
#5
Re: Interpreting return code of recv() socket api
Yes , zero is returned when servre gracefully closes the connection.
In addition to this , I am also getting zero in main thread in recv when another thread on client drops the socket and I see no way to differentiate between the two cases.
-
August 6th, 2012, 02:47 AM
#6
Re: Interpreting return code of recv() socket api
Originally Posted by yaniv135
I am also getting zero in main thread in recv when another thread on client drops the socket and I see no way to differentiate between the two cases.
Please, define "... drops the socket"
Victor Nijegorodov
-
August 6th, 2012, 03:28 AM
#7
Re: Interpreting return code of recv() socket api
shutdown() [read ] followed by close().
-
August 6th, 2012, 03:35 AM
#8
Re: Interpreting return code of recv() socket api
Originally Posted by yaniv135
shutdown() [read ] followed by close().
But it is what is called "gracefully closed connection"!
Therefore you don't get any error code after calling recv().
Victor Nijegorodov
-
August 6th, 2012, 03:56 AM
#9
Re: Interpreting return code of recv() socket api
ok.
Then as I understand , there would be no way to distinguish if the connection is closed gracefully by the server or by the client as recv() at client
will have return value 0.
-
August 6th, 2012, 04:04 AM
#10
Re: Interpreting return code of recv() socket api
Sorry, I can't get you...
If s server closes the connection then it must know that it does (is doing) it!
If it has NOT closed the connection yet, but recv() returns zero, then the connection was closed by another side (by client).
And the same - for the client...
Victor Nijegorodov
-
August 6th, 2012, 03:30 PM
#11
Re: Interpreting return code of recv() socket api
Both are graceful closures, so by use of socket functions alone, you will not be able to determine whether the socket was closed "normally" by the server or by you yourself, in another thread, because of a detected timeout.
Add another way to communicate this information. You might, for example, already have an array of some sort, which stores SOCKET information. Add another member to the array, which flags whether the socket was closed by you due to timeout, and which the reading thread checks whenever it receives a "0" return value from recv().
Mike
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|