Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Death of a socket changes its behavior in several ways, so these methods are both valid :)</p> <p>With both methods you actually check those parts of the socket's behavior that change after disconnection.</p> <blockquote> <p>I don't really understand his statement about calling Receive() to make sure that the remote endpoint has actually received all the data I sent. (Do sockets block receiving until the sending buffer is empty?)</p> </blockquote> <p><code>TCP</code> is reliable protocol, that means that every packet you send must be acknowledged. Acknowledgement implies sending the packets with <code>ACK</code> bit set. These packets may or may not contain additional (payload) data.</p> <p>When the socket is connected, <code>Receive()</code> will block until the socket receives a packet with non-empty payload. But when the socket is disconnected, <code>Receive()</code> will return as soon as the last <code>ACK</code> packet arrives.</p> <p>Calling <code>Receive()</code> ensures that you either <em>receive</em> that last <code>ACK</code> packet from your remote endpoint or a disconnect timeout occurs and you will be able to <em>receive</em> nothing more on this socket.</p> <blockquote> <p>The example on the same page shows how to do it. (I wonder why does it allocate a 1-byte array, even though it calls Send with 0 length?) But a post by Ian Griffiths says that I should read from the socket, not send through it.</p> </blockquote> <p>When <code>send()</code>ing to a socket, you actually try to append some data to the end of the socket queue. Is there is some place left in the buffer, then your <code>Send()</code> returns instantly, if not, the <code>Send()</code> blocks until there is some place.</p> <p>When the socket is in disconnected state, <code>TCP/IP</code> stack prevents all further operations with the buffer, that's why <code>Send()</code> returns an error.</p> <p><code>Send()</code> implements a basic pointer check, this means it fails when a <code>NULL</code> pointer is passed to it. You may probably pass any non-null constant as a pointer, but you better allocate 1 byte instead of making the constant up — just in case.</p> <hr> <p>You may use any method you like, as none of them is resource consuming. As long as they are used for socket connection checking, they are identical.</p> <p>As for me, I would prefer <code>Receive()</code>, as this is what you normally run in a cycle and wait for. You get a non-zero from <code>Receive()</code>, you process data; you get a zero, you process disconnection.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload