Note that there are some explanatory texts on larger screens.

plurals
  1. PO.Net Socket doesn't respond to remote disconnect?
    primarykey
    data
    text
    <p>I'm writing a small (C#) client application that sends data using a TCP/IP connection to a remote server. I'm using the standard .Net TcpClient object and want to leave the connection open from the client end as I am regularly submitting data packets to the server. However, it is possible that the server may close the connection, in which case I need to know to re-connect before sending my next packet.</p> <p>Using Wireshark, I can see (only) the following dialogue when the server terminates the connection:</p> <p><code>server >>> FIN, ACK<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ACK &lt;&lt;&lt; client </code></p> <p>What I do <em>not</em> see is my client responding with a FIN of its own, to complete the connection shutdown. The result is that my client program only finds out that the connection is down after sending the next data packet.</p> <p>Is there any way I can set up TcpClient or its underlying Socket so as to complete the disconnect, and provide some feedback so that my client code knows to re-connect before sending the next packet?</p> <p><strong>Added</strong> in response to comment below: My sending code is very simple - the object that maintains the TcpClient and NetworkStream member variables, has a member function containing (essentially) the following:</p> <pre><code>bool sent = false; byte[] buffer = Encoding.UTF8.GetBytes(dataString); while (!sent) { try { m_outStream.Write(buffer, 0, buffer.Length); sent = true; } catch (Exception ex) { if (m_outStream != null) { m_outStream.Dispose(); } m_client = new TcpClient(AddressFamily.InterNetwork); m_client.Connect(ipAddress, ipPort); m_outStream = m_client.GetStream(); } } </code></pre> <p>With m_client and m_outStream initialized, this simply performs a single pass every time. Then using Wireshark I can see the server send a packet with flags <code>FIN, ACK</code> to which the client responds with <code>ACK</code>.</p> <p>The next time I call my function, the data is sent out with <code>PSH, ACK</code>, and the server responds with <code>RST, ACK</code> but does not read the incoming data. No exception is raised by the client.</p> <p>Then I call my function a second time, and an exception is raised causing the connection to be re-started.</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. 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