CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Aug 2012
    Posts
    5

    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.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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

  3. #3
    Join Date
    Aug 2012
    Posts
    5

    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.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Interpreting return code of recv() socket api

    Quote Originally Posted by yaniv135 View Post
    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

  5. #5
    Join Date
    Aug 2012
    Posts
    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.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Interpreting return code of recv() socket api

    Quote Originally Posted by yaniv135 View Post
    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

  7. #7
    Join Date
    Aug 2012
    Posts
    5

    Re: Interpreting return code of recv() socket api

    shutdown() [read ] followed by close().

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Interpreting return code of recv() socket api

    Quote Originally Posted by yaniv135 View Post
    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

  9. #9
    Join Date
    Aug 2012
    Posts
    5

    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.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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

  11. #11
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    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
  •  





Click Here to Expand Forum to Full Width

Featured