TcpClient close() method socket leak

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel

    TcpClient close() method socket leak

    TcpClient close() method socket leak

    when i use TcpClient to open a connection, send data and close the TcpClient
    with myTcpClientInst ance.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.Linger State = lingerOption;
    myclient.NoDela y = true;
    myclient.Receiv eTimeout = 0;
    myclient.Connec t("foobox", 8888);
    NetworkStream networkStream ;
    networkStream = myclient.GetStr eam();
    StreamWriter streamWriter ;
    streamWriter = new StreamWriter(ne tworkStream);
    string strData = "";
    strData += "helloworld \0";
    streamWriter.Wr iteLine(strData );
    streamWriter.Fl ush();
    streamWriter.Cl ose() ;
    networkStream.C lose();
    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(808 2);
    tcpListener.Sta rt();
    Console.WriteLi ne("Server Started") ;
    while(true)
    {
    Socket socketForClient = tcpListener.Acc eptSocket();
    try
    {
    if(socketForCli ent.Connected)
    {
    Console.WriteLi ne("Client connected");
    NetworkStream networkStream = new NetworkStream(s ocketForClient) ;
    StreamReader streamReader = new StreamReader(ne tworkStream);
    string line = streamReader.Re adLine();
    streamReader.Cl ose();
    Console.WriteLi ne("Read:" +line);
    }
    socketForClient .Shutdown(Syste m.Net.Sockets.S ocketShutdown.B oth);
    socketForClient .Close();
    Console.WriteLi ne("Client disconnected");
    GC.Collect();
    Console.WriteLi ne("Garbage Collected");
    }
    catch(Exception e)
    {
    Console.WriteLi ne(e.ToString() ) ;
    }
    }
    }


  • Daniel

    #2
    Re: TcpClient close() method socket leak

    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" <softwareengine er98037@yahoo.c om> wrote in message
    news:%232BHexHE FHA.2540@TK2MSF TNGP09.phx.gbl. ..[color=blue]
    > TcpClient close() method socket leak
    >
    > when i use TcpClient to open a connection, send data and close the[/color]
    TcpClient[color=blue]
    > with myTcpClientInst ance.Close(); it takes 60 seconds for the actual[/color]
    socket[color=blue]
    > 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[/color]
    not[color=blue]
    > 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.[/color]
    I[color=blue]
    > 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.Linger State = lingerOption;
    > myclient.NoDela y = true;
    > myclient.Receiv eTimeout = 0;
    > myclient.Connec t("foobox", 8888);
    > NetworkStream networkStream ;
    > networkStream = myclient.GetStr eam();
    > StreamWriter streamWriter ;
    > streamWriter = new StreamWriter(ne tworkStream);
    > string strData = "";
    > strData += "helloworld \0";
    > streamWriter.Wr iteLine(strData );
    > streamWriter.Fl ush();
    > streamWriter.Cl ose() ;
    > networkStream.C lose();
    > 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(808 2);
    > tcpListener.Sta rt();
    > Console.WriteLi ne("Server Started") ;
    > while(true)
    > {
    > Socket socketForClient = tcpListener.Acc eptSocket();
    > try
    > {
    > if(socketForCli ent.Connected)
    > {
    > Console.WriteLi ne("Client connected");
    > NetworkStream networkStream = new NetworkStream(s ocketForClient) ;
    > StreamReader streamReader = new StreamReader(ne tworkStream);
    > string line = streamReader.Re adLine();
    > streamReader.Cl ose();
    > Console.WriteLi ne("Read:" +line);
    > }
    > socketForClient .Shutdown(Syste m.Net.Sockets.S ocketShutdown.B oth);
    > socketForClient .Close();
    > Console.WriteLi ne("Client disconnected");
    > GC.Collect();
    > Console.WriteLi ne("Garbage Collected");
    > }
    > catch(Exception e)
    > {
    > Console.WriteLi ne(e.ToString() ) ;
    > }
    > }
    > }
    >
    >[/color]


    Comment

    • Mike Junkin

      #3
      Re: TcpClient close() method socket leak

      The TIME_WAIT state for the closed socket is normal. Even your 'c' program should
      have had sockets in the TIME_WAIT state after they where closed. Some links to
      explanations of this:



      "NOTE: It is normal to have a socket in the TIME_WAIT state for a long period of
      time. The time is specified in RFC793 as twice the Maximum Segment Lifetime (MSL).
      MSL is specified to be 2 minutes. So, a socket could be in a TIME_WAIT state for
      as long as 4 minutes. Some systems implement different values (less than 2
      minutes) for the MSL"



      "Problem: Netstat shows lots of sockets in the TIME_WAIT state. What's wrong?"
      "Solution: Nothing's wrong. TIME_WAIT is absolutely normal. [clipped] Thus,
      TIME_WAIT usually lasts 30-120 seconds"

      On the other hand I'm not sure why your would die with 3k of socket outstanding.
      That seems a little low although the max number is dependent on OS memory so it
      varies from machine to machine.

      There is a 'resuse address' option also for sockets but I'm not sure if that would
      help.

      Mike

      --
      POST BY: http://www.dotNET.us - Need .NET? Just ask, Please dotNET.us

      Comment

      • Arthur M.

        #4
        Re: TcpClient close() method socket leak

        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:
        [color=blue]
        > 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" <softwareengine er98037@yahoo.c om> wrote in message
        > news:%232BHexHE FHA.2540@TK2MSF TNGP09.phx.gbl. ..[color=green]
        > > TcpClient close() method socket leak
        > >
        > > when i use TcpClient to open a connection, send data and close the[/color]
        > TcpClient[color=green]
        > > with myTcpClientInst ance.Close(); it takes 60 seconds for the actual[/color]
        > socket[color=green]
        > > 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[/color]
        > not[color=green]
        > > 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.[/color]
        > I[color=green]
        > > 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.Linger State = lingerOption;
        > > myclient.NoDela y = true;
        > > myclient.Receiv eTimeout = 0;
        > > myclient.Connec t("foobox", 8888);
        > > NetworkStream networkStream ;
        > > networkStream = myclient.GetStr eam();
        > > StreamWriter streamWriter ;
        > > streamWriter = new StreamWriter(ne tworkStream);
        > > string strData = "";
        > > strData += "helloworld \0";
        > > streamWriter.Wr iteLine(strData );
        > > streamWriter.Fl ush();
        > > streamWriter.Cl ose() ;
        > > networkStream.C lose();
        > > 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(808 2);
        > > tcpListener.Sta rt();
        > > Console.WriteLi ne("Server Started") ;
        > > while(true)
        > > {
        > > Socket socketForClient = tcpListener.Acc eptSocket();
        > > try
        > > {
        > > if(socketForCli ent.Connected)
        > > {
        > > Console.WriteLi ne("Client connected");
        > > NetworkStream networkStream = new NetworkStream(s ocketForClient) ;
        > > StreamReader streamReader = new StreamReader(ne tworkStream);
        > > string line = streamReader.Re adLine();
        > > streamReader.Cl ose();
        > > Console.WriteLi ne("Read:" +line);
        > > }
        > > socketForClient .Shutdown(Syste m.Net.Sockets.S ocketShutdown.B oth);
        > > socketForClient .Close();
        > > Console.WriteLi ne("Client disconnected");
        > > GC.Collect();
        > > Console.WriteLi ne("Garbage Collected");
        > > }
        > > catch(Exception e)
        > > {
        > > Console.WriteLi ne(e.ToString() ) ;
        > > }
        > > }
        > > }
        > >
        > >[/color]
        >
        >
        >[/color]

        Comment

        Working...