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

    select in one thread, closesocket in another thread

    I got a quick question: if a thread is waiting in a select(...) and another thread calls closesocket, will the select(...) returns a error?

  2. #2
    Join Date
    Mar 2001
    Posts
    2,529

    Re: select in one thread, closesocket in another thread

    Yes the call to select will return -1 if there is a problem with one of the sockets.

    Since you are using blocking sockets, you had best not close the socket from a seperate thread.

    In non-blocking scenarios it is much easier to handle this gracefully, by simply setting the timeout on the select to zero.

    Note the flow chart for select states.

    http://publib.boulder.ibm.com/infoce...6xnonblock.htm
    ahoodin
    To keep the plot moving, that's why.

  3. #3
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: select in one thread, closesocket in another thread

    Quote Originally Posted by ahoodin View Post
    Since you are using blocking sockets, you had best not close the socket from a seperate thread.

    In non-blocking scenarios it is much easier to handle this gracefully, by simply setting the timeout on the select to zero.
    What has the former to do with the latter?
    It does not matter whether the sockets passed to select() are blocking or nonblocking. The result of closing a socket would be identical: select() would return with an indication that there is something to be received on the socket and a subsequent recv() would return 0 to indicate that the socket is closed. select() does not return -1 here!

    The described scenario is perfectly legal IMHO.

    Regards,
    Richard

  4. #4
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: select in one thread, closesocket in another thread

    I hvae to revise my statement: when select() returns, a subsequent call to recv() would return -1 (error indication) because the socket has locally been closed (by the other thread).

  5. #5
    Join Date
    Mar 2001
    Posts
    2,529

    Re: select in one thread, closesocket in another thread

    Well that can be especially bad depending on the way this lad has coded his app. If that happens, the process may exit. Personally I believe that one should not close the socket until communications are done. It is bad to just call close socket at any point in communications.
    ahoodin
    To keep the plot moving, that's why.

  6. #6
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: select in one thread, closesocket in another thread

    But what's the point of your statement? Assuming you have one thread only responsible for reading data from the socket (or several sockets, or you would not need select()) and another thread for some higher logic, then why not close the socket when the latter thread decides you're done with communication? The first thread gets to know the socket is no longer valid and terminates.

  7. #7
    Join Date
    Jan 2005
    Location
    Akron, Ohio
    Posts
    670

    Re: select in one thread, closesocket in another thread

    Would the situation be similar with connect() and closesocket() . . . specifically: if one thread closes a socket that another thread is attempting a connect() on, does the second thread continue to block with connect() or does it promptly return a -1? If so, what WSAGetLastError() would it result with?
    error C2146a : syntax error : nebulizer stained in the tower floppy apple rider. Go rubble in flee smite. Bleeble snip snip.

    Documentation says: error C2146a - This means there is an error somewhere in the course of human endeavor. Fix in the usual way.

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

    Re: select in one thread, closesocket in another thread

    Quote Originally Posted by paradoxresolved View Post
    Would the situation be similar with connect() and closesocket() . . . specifically: if one thread closes a socket that another thread is attempting a connect() on, does the second thread continue to block with connect() or does it promptly return a -1? If so, what WSAGetLastError() would it result with?
    Yes, the principle is the same, and I believe that the WSAGetLAstError will return WSAENOTSOCK

    Incidentally, it probably would have been better to start a new thread that references this one, rather than awakening a zombie thread more than three years old.

    Mike

  9. #9
    Join Date
    Jan 2005
    Location
    Akron, Ohio
    Posts
    670

    Re: select in one thread, closesocket in another thread

    Fair enough. It just seemed like an almost identical question. Thanks for your help!
    error C2146a : syntax error : nebulizer stained in the tower floppy apple rider. Go rubble in flee smite. Bleeble snip snip.

    Documentation says: error C2146a - This means there is an error somewhere in the course of human endeavor. Fix in the usual way.

  10. #10
    Join Date
    Mar 2001
    Posts
    2,529

    Re: select in one thread, closesocket in another thread

    This Zombie thread has been harvested.
    ahoodin
    To keep the plot moving, that's why.

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