Re: TcpClient close() method socket leak

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



TIME_WAIT is normal behaviour for proper TCP session closure.


Reference blurb from RFC 793 with explanation as to why.
The dirty way to close it is to use RST TCP packet header flag and kill the
connection instead of shutting it down. This is not something you would
consider to be "proper way"
The reason as to why TCP_WAIT is hanging around for 60 seconds - berkley
implementation of a socket states Maximum Segment Lifetime to be 30 seconds,
hence 2 times that is 60 (read below)

The protocol places no restriction on a particular connection being
used over and over again. A connection is defined by a pair of
sockets. New instances of a connection will be referred to as
incarnations of the connection. The problem that arises from this is
-- "how does the TCP identify duplicate segments from previous
incarnations of the connection?" This problem becomes apparent if the
connection is being opened and closed in quick succession, or if the
connection breaks with loss of memory and is then reestablished.

To avoid confusion we must prevent segments from one incarnation of a
connection from being used while the same sequence numbers may still
be present in the network from an earlier incarnation. We want to
assure this, even if a TCP crashes and loses all knowledge of the
sequence numbers it has been using. When new connections are created,
an initial sequence number (ISN) generator is employed which selects a
new 32 bit ISN. The generator is bound to a (possibly fictitious) 32
bit clock whose low order bit is incremented roughly every 4
microseconds. Thus, the ISN cycles approximately every 4.55 hours.
Since we assume that segments will stay in the network no more than
the Maximum Segment Lifetime (MSL) and that the MSL is less than 4.55
hours we can reasonably assume that ISN's will be unique.


"Daniel" wrote:

> also, even after the client executable is unloaded from memory the ever evil
> socket entries remain for 60 seconds:
> [System Process]:0 TCP foobox:8888 localhost:2188 TIME_WAIT
> [System Process]:0 TCP foobox:8888 localhost:2189 TIME_WAIT
> [System Process]:0 TCP foobox:8888 localhost:2190 TIME_WAIT
>
> is it that i can only do 3000 open, send, close in every 60 seconds per a
> limitation w/ the TcpClient or is there something else i could try?
>
> "Daniel" <softwareengineer98037@xxxxxxxxx> wrote in message
> news:%232BHexHEFHA.2540@xxxxxxxxxxxxxxxxxxxxxxx
> > TcpClient close() method socket leak
> >
> > when i use TcpClient to open a connection, send data and close the
> TcpClient
> > with myTcpClientInstance.Close(); it takes 60 seconds for the actual
> socket
> > on the client machine to close per my network app the computer fills up w/
> > thousands of these
> >
> > [System Process]:0 TCP foobox:8888 localhost:2188 TIME_WAIT
> > [System Process]:0 TCP foobox:8888 localhost:2189 TIME_WAIT
> > [System Process]:0 TCP foobox:8888 localhost:2190 TIME_WAIT
> > [System Process]:0 TCP foobox:8888 localhost:2191 TIME_WAIT
> > [System Process]:0 TCP foobox:8888 localhost:2192 TIME_WAIT
> > [System Process]:0 TCP foobox:8888 localhost:2193 TIME_WAIT
> >
> > until the "Only one usage of each socket address" error occures. How to
> > work around this? I need to open connect, send data, and close it. i can
> not
> > share connections in my case i have to open, send and close and have the
> > close truely close. and i have to do more then 3k of these in 60 seconds.
> I
> > know this is possible because I have a c version of the client that uses c
> > sockets and it works fine and does not fill with thousands of:
> >
> > [System Process]:0 TCP foobox:8888 localhost:2188 TIME_WAIT
> >
> > Is this a limitation of TcpClient ? or is there a way to truely close a
> > TcpClient imediatly? i tried setting linger option false, no delay true,
> > timeout 0 etc. still no progress the client sockets all get used up w/
> > thousands of
> >
> > [System Process]:0 TCP foobox:8888 localhost:2188 TIME_WAIT
> > [System Process]:0 TCP foobox:8888 localhost:2189 TIME_WAIT
> > [System Process]:0 TCP foobox:8888 localhost:2190 TIME_WAIT
> >
> > until the "Only one usage of each socket address" error occures.
> >
> > here is what my client code looks like:
> >
> > TcpClient myclient;
> > myclient = new TcpClient();
> > LingerOption lingerOption = new LingerOption (false, 0);
> > myclient.LingerState = lingerOption;
> > myclient.NoDelay = true;
> > myclient.ReceiveTimeout = 0;
> > myclient.Connect("foobox", 8888);
> > NetworkStream networkStream ;
> > networkStream = myclient.GetStream();
> > StreamWriter streamWriter ;
> > streamWriter = new StreamWriter(networkStream);
> > string strData = "";
> > strData += "helloworld\0";
> > streamWriter.WriteLine(strData);
> > streamWriter.Flush();
> > streamWriter.Close() ;
> > networkStream.Close();
> > myclient.Close();
> >
> > something missing? should i do more then close? i tried shutdown too.. no
> > progress
> >
> > here is the dummy server code if it matters:
> >
> > public static void Main()
> > {
> > TcpListener tcpListener = new TcpListener(8082);
> > tcpListener.Start();
> > Console.WriteLine("Server Started") ;
> > while(true)
> > {
> > Socket socketForClient = tcpListener.AcceptSocket();
> > try
> > {
> > if(socketForClient.Connected)
> > {
> > Console.WriteLine("Client connected");
> > NetworkStream networkStream = new NetworkStream(socketForClient);
> > StreamReader streamReader = new StreamReader(networkStream);
> > string line = streamReader.ReadLine();
> > streamReader.Close();
> > Console.WriteLine("Read:" +line);
> > }
> > socketForClient.Shutdown(System.Net.Sockets.SocketShutdown.Both);
> > socketForClient.Close();
> > Console.WriteLine("Client disconnected");
> > GC.Collect();
> > Console.WriteLine("Garbage Collected");
> > }
> > catch(Exception e)
> > {
> > Console.WriteLine(e.ToString()) ;
> > }
> > }
> > }
> >
> >
>
>
>
.



Relevant Pages

  • Re: about the TIME_WAIT
    ... When a socket is closed, ... Assume that a connection is in ESTABLISHED state, ... whether the client got the data, but that is not an issue for TCP; ... where data may or may not have been lost. ...
    (perl.beginners)
  • Closing socket during async Receive
    ... I am trying to write a Tcp "Server" that opens a class that wraps a tcp ... socket when a new connection is made ). ... Everything is going swimmingly except when I try to close the socket ... public void Close ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Detecting when a socket has been closed.
    ... shutdown the server closing the socket the client still thinks the socket is open. ... First send will not throw exception, because there is no way for TCP stack to know that other end has actually closed its receiving side. ... It takes some time for packet to reach other side and return information that server side is gone. ... in case other end has closed socket and network is still operating after first send TCP RST segment will be returned to TCP stack notifying it connection has gone down. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: MVPs... Sockets.. Killing!!
    ... behavior of TCP and WinSock This and related issues have ... winsock sends FD_Close when the connection drops *normally*. ... and accepts incoming connection in a socket and returns ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Need Help with IOCP
    ... In my proxy, I accept connections, connect to a web server, make a request. ... The outgoing socket is closed by the web server. ... close the connection to the web browser or it will sit there indefinitely ... The TCP ACK stuff is what the packet sniffer found, ...
    (microsoft.public.win32.programmer.networks)