none
Windows tcp table not allowing to inspect all occupied ports RRS feed

  • Question

  • I try to build an app, monitoring application consumption of occupied dynamic ports. In order to inspect tcp table content, I use netstat and winapi's GetExtendedTcpTable, which I believe is actually used by netstat.

    I noticed unobvious behaviour of tcp table. If listener side doesn't properly disposes client but client gets finalized by GC, I expect in such case connection to hang in ESTABLISHED state in tcp table, because TcpClient destructor doesn't close socket. The actual behavior is that connection disappears from tcp table but it seems like port still remains occupied, because if I continue establish more connections, after 16k connections I'll get an SocketException (10055), indicating that I'm out of free ports.

    Code below reproduces this issue. Within infinite loop client-server connections are established in batches of 2k with period of 1 minute. I expect that if I start app, wait 7 minutes and call netstat or GetExtendedTcpTable I'll see 14k ESTABLISHED connections. But when I do it I actually see that this number varies in range 1-5k and never goes above. So it comes to the final problem: app crashes in about 8 minutes with exception of exhausted tcp ports though netstat nor tcp table don't show any sign of port exhaustion.

    var l = new TcpListener(IPAddress.Loopback, 0);
    l.Start();
    Task.Factory.StartNew(() =>
                              {
                                  while (true)
                                  {
                                     // accepted TcpClients are periodically finalized by GC (without freeing socket connection)
                                     l.AcceptTcpClient();
                                 }
                             });
    
    // must remember clients' references, otherwise, GC finalizes clients and since server side also finalizes accepted clients, tcp connection gets utilized
    var clients = new List<TcpClient>();
    while (true)
    {
        for (var i = 0; i < 2000; i++)
        {
            clients.Add(new TcpClient());
            clients.Last().Connect(IPAddress.Loopback, ((IPEndPoint) l.LocalEndpoint).Port);
        }
        Task.Delay(1000).GetAwaiter().GetResult();
    }

    Monday, September 16, 2019 6:40 AM

Answers

All replies

  • Hi pavel trofimov

    Thank you for posting here.

    I make a test on my side, and I get the following exception:

    I make a change to my code, and I get no exception.

    Here's my code:

                var clients = new List<TcpClient>();
                while (true)
                {
                    for (var i = 0; i < 2000; i++)
                    {
                        clients.Add(new TcpClient());
                        clients.Last().Connect(IPAddress.Loopback, ((IPEndPoint)l.LocalEndpoint).Port);
                        clients.Clear();
                    }
                    Task.Delay(1000).GetAwaiter().GetResult();
                }

    The List<T>.Clear Method will remove all the elements in the list.

    Besides, I find a reference to close TcpClient connection.

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

    Hope them can help you

    Best Regards,

    Xingyu Zhao





    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.






    Tuesday, September 17, 2019 10:24 AM
    Moderator
  • Thank you for your answer.

    Maybe I wasn't clear on my issue. As my comment about client references states, I understand that if you clear list all clients get finalized and sockets are released and become available for reusing. The point is that if you do not clear list, tcp connections and underlying sockets are obviously not released, but with `netstat` or `GetExtendedTcpTable` I can not inspect that sockets are occupied.

    Main problem is that some code may lead to situation when tcp sockets are locked, but OS is not able to provide me with any information of such situation.


    Thursday, September 19, 2019 8:34 AM
  • Hi pavel trofimov,

    Thanks for your feedback.

    Event Viewer can provide the information when tcp sockets are locked.

    Here's the result in Event Viewer when I get the exception.

    Besides, you can refer to the following reference about Event Viewer.

    What Is the Windows Event Viewer, and How Can I Use It?

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, September 20, 2019 9:56 AM
    Moderator
  • Thanks again for response.

    Yes, you are right, Event Viewer provides me with info, that port exhaustion happened. But there is no way to trace which process has consumed all ports, is it? I do not understand the reason netstat and ExtendedTcpTable do not indicate that ports are actually consumed by my user process.

    Ultimate goal is to detect process which consumes majority of occupied ports when port exhaustion happens and kill it. I know that in some cases it is not possible, for example, whet all ports hang in TIME_WAIT state which have system PID = 0. But my code does not lead to such TIME_WAIT state. I expect my code to cause connections to hang in table with state of ESTABLISHED, because server nor client do not release sockets. And ESTABLISHED connections usually have correct PID.

    Can you say why netstat and ExtendedTcpTable fail to provide PID info and is there any other tool able of doing it?

    Friday, September 20, 2019 11:11 AM
  • Hi pavel trofimov,

    I have found a related reference.

    C# Sample to list all the active TCP and UDP connections using Windows Form appl

    Here's my result of test.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, September 27, 2019 8:41 AM
    Moderator