Like Tree4Likes
  • 1 Post By gemera
  • 1 Post By vart
  • 1 Post By gemera
  • 1 Post By gemera

recv() after disconnect

This is a discussion on recv() after disconnect within the Networking/Device Communication forums, part of the General Programming Boards category; I'm trying to send data through the internet. Is the recv() function supposed to receive all data after the send() ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749

    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++ 2010 with Code::Blocks.

  2. #2
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    235
    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.
    Ducky likes this.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,459
    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.
    Ducky likes this.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749
    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++ 2010 with Code::Blocks.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    235
    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.
    Ducky likes this.

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

  7. #7
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749
    Thanks.
    Compiler MSVC++ 2010 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, 08: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, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21