Thread: recv() after disconnect

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    897

    recv() after disconnect

    I'm trying to send data through the internet.

    Is the recv() function supposed to receive all data after the send() function has sent all and disconnected after?
    Or are the two sides supposed to stay connected until the recv() side received all bytes?
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    I believe the short answer is not necessarily so.

    The solution is to use setsockopt() with the SO_LINGER option which means close()
    blocks until the closing handshake completes.

    If you google on SO_LINGER you'll probably get a good explanation of why.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    What type of socket you are using?

    For TCP Close causes to put the FIN packet in the outgoing stream, so the other side will have to read all the data before reaching this FIN packet. Of cause after closing the sender side socket you will not be able to receive transmission error notifications in case of asynchronous socket.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    897
    I'm using TCP socket.
    Actually it will still deliver on a LAN after disconnect() is called even without SO_LINGER.
    But it wont over the Internet.

    I'm still to try SO_LINGER through the Internet.

    Apparently SO_LINGER is broken, see bottom of the page.
    Graceful Shutdown, Linger Options, and Socket Closure (Windows)
    Compiler MSVC++ 2013 with Code::Blocks.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    I think this article covers all the angles well.

    The reset action of TCP on close() if there is still data (even a stray byte) in the RECVQ is something to be aware of.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    btw my reading of the bug report is that the problem is with WSASendDisconnect() not SO_LINGER.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    897
    Thanks.
    Compiler MSVC++ 2013 with Code::Blocks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking for server disconnect
    By Martin_T in forum Networking/Device Communication
    Replies: 1
    Last Post: 11-26-2009, 09:57 PM
  2. Disabling a network interface card to disconnect from LAN..
    By elroy in forum Networking/Device Communication
    Replies: 8
    Last Post: 05-16-2007, 12:53 AM
  3. Disconnect the Internet
    By Queatrix in forum C++ Programming
    Replies: 5
    Last Post: 12-20-2005, 10:20 PM
  4. Monitor For Network Disconnect
    By Epo in forum C++ Programming
    Replies: 1
    Last Post: 06-16-2004, 10:35 PM
  5. force modem disconnect or power down
    By iain in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-17-2001, 06:17 PM