How to properly and completely close / reset a TcpClient connection?

What is the correct way to close or reset a TcpClient connection? We have software that communicates with the equipment, but sometimes something goes wrong, and we no longer communicate with it until we restart the software.

I tried to force TcpClient.Close () and even set it to null, but this does not work. Only a complete restart of the software is performed.

Suggestions?




I cannot use the using keyword because TpcClient is defined in only one place, but is used throughout the library. (And there is only one connection at any given time)

This is a library that handles communication. The software itself can call the ResetConnection () method of the Controller class (which is hardware).

Currently it looks like

if (tcpClient != null) { tcpClient.Close(); tcpClient = null; } 

Now from what I read here, I have to use tcpClient.Dispose () instead of "= null"

I will give this attempt and see if it matters.

+64
c # tcp
Jan 08 '09 at 17:55
source share
8 answers

You must close the stream before closing the connection:

 tcpClient.GetStream().Close(); tcpClient.Close(); 

Closing a client does not close the stream.

+73
Dec 30 '09 at 14:44
source share

Given that the accepted answer is outdated and I don’t see anything in the other answers about this, I’m creating a new one. In .Net 2 and earlier, you had to manually close the stream before closing the connection. This error is fixed in all later versions of TcpClient in C #, and, as indicated in the document on the Close method, calling the Close method closes both the connection and the stream

EDIT according to Microsoft Docs

The Close method marks the instance as remote and requests that the corresponding socket close the TCP connection. Based on the LingerState property, a TCP connection may remain open for some time after calling the Close method when data has yet to be sent. A notification is not provided when the underlying connection has completed the close.

Calling this method will ultimately close the corresponding socket and also close the associated NetworkStream, which is used to send and receive data if it was created.

+21
Mar 22 '16 at 9:59
source share

Use the word: using . Good habit to program.

 using (TcpClient tcpClient = new TcpClient()) { //operations tcpClient.Close(); } 
+20
Jan 08 '09 at 18:42
source share

Closes the socket connection and allows reusing the socket:

 tcpClient.Client.Disconnect(false); 
+7
Aug 13 '09 at 23:57
source share

With the exception of some internal protocols, Close == Dispose.

Dispose calls tcpClient.Client.Shutdown (SocketShutdown.Both), but it eats any errors. Perhaps if you call this directly, you might get useful information about the exception.

+4
Jan 09 '09 at 14:35
source share

The correct way to close the socket so you can reopen:

 tcpClient.Client.Disconnect(true); 

The boolean parameter indicates whether you want to reuse the socket:

Using Disconnect Method

+4
Jun 24 '16 at 6:25
source share

Despite all the relevant using statements, calling Close , having some exponential logic fallback and recreating TcpClient I still had problems when the application cannot restore the TCP connection without restarting the application. It continues to fail with System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host .

But there is a LingerState option on TcpClient which seems that it may have solved the problem (it may not be known for several months, since my own hardware setup just doesn't work about that often!). See MSDN .

 // This discards any pending data and Winsock resets the connection. LingerOption lingerOption = new LingerOption(true, 0); using (var tcpClient = new TcpClient {SendTimeout = 2000, ReceiveTimeout = 2000, LingerState = lingerOption }) ... 
+2
Jan 17 '18 at 16:34
source share

Have you tried calling TcpClient.Dispose () explicitly?

And are you sure you have TcpClient.Close () and TcpClient.Dispose () - ed ALL connections?

0
Jan 08 '09 at 18:16
source share



All Articles