c# - What exactly do socket's Shutdown, Disconnect, Close and Dispose do? -


it's surprisingly hard find simple explanation on these 4 methods actually do, aimed @ network programming newbies. people state believe proper way close socket in particular scenario, not happens in background behind each step.

going teach-a-man-to-fish philosophy, can explain shutdown, disconnect, close , dispose methods?

an answer on stackoverflow made me think have reached glimpse of understanding. went testing bit , here's summary of newbie's view. please correct me if i'm wrong because based on inference, not expertise.

shutdown

shutdown disables send and/or receive methods, depending on provided argument. doesn't disable underlying protocol handling , never blocks.

if send disabled, queues zero-byte send packet underlying send buffer. when other side receives packet, knows socket no longer send data.

if receive disabled, data other side might trying send lost.

if receive disabled without disabling send, prevents socket receiving data. since no zero-byte packet sent, other side won't know until tries send something, , if socket's protocol requires acknowledging.

disconnect

first, disconnect equivalent of shutdown(socketshutdown.both).

then blocks, waiting 2 things:

  1. for queued-up send data sent.
  2. for other side acknowledge zero-byte packet (if applicable underlying protocol).

if call disconnect(false), system resources freed.

close

close frees system resources. may abruptly stop sending queued-up data. if called argument, wait data sent, specified timeout.

dispose

dispose same close overload without timeout argument. more precise, close without timeout same dispose.

if use using block on socket, automatically call dispose.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -