3

From what I understand, those lines:

var client = await listener.AcceptTcpClientAsync();

var client = listener.AcceptTcpClient();

should work the same way, but if I try to open, for example, 5k connections (without closing them), very soon listener (async) either stops responding or throws exception.

Anyone have idea what may be wrong?

Sample class:

class ListenerTest
{
    static int connectionNumber = 0;
    static int port = 23456;

    public void StartListening(bool async)
    {
        var listener = new TcpListener(IPAddress.Any, port);
        listener.Start();

        if (async)
        {
            var task = HandleConnectionsAsync(listener);
            task.Wait();
        }
        else
            HandleConnections(listener);
    }

    async Task HandleConnectionsAsync(TcpListener listener)
    {
        while (true)
        {
            Console.Write("Waiting for async connection...");
            var client = await listener.AcceptTcpClientAsync();
            Console.WriteLine("OK #" + connectionNumber);
            connectionNumber++;
        }
    }

    void HandleConnections(TcpListener listener)
    {
        while (true)
        {
            Console.Write("Waiting for sync connection...");
            var client = listener.AcceptTcpClient();
            Console.WriteLine("OK #" + connectionNumber);
            connectionNumber++;
        }
    }

    public static void ConnectTest(int count)
    {
        var ep = new IPEndPoint(IPAddress.Loopback, port);
        for (var i = 0; i < count; i++)
            new TcpClient().Connect(ep);
    }
}

Usage:

static void Main(string[] args)
{
    var isAsync = true;
    ThreadPool.QueueUserWorkItem(new WaitCallback(StartListening), isAsync);
    ListenerTest.ConnectTest(5000);
}

static void StartListening(object state)
{
    var my = new ListenerTest();
    my.StartListening((bool)state);
}
3
  • No; one of this is blocking and wastes a thread.
    – SLaks
    Commented Jul 8, 2013 at 14:45
  • What does the exception say?
    – svick
    Commented Jul 8, 2013 at 15:06
  • Try to reproduce yourself and see ;) When testing inside same program instance - then TcpListener throws SocketException "An existing connection was forcibly closed by the remote host" after accepting about 2500 connections. If TcpListener and TcpClients are in different program instances, then TcpListener just stops accepting connections without any exception and TcpClient throws SocketException "An existing connection was forcibly closed by the remote host" after making about 120 connections.
    – Savvkin
    Commented Jul 8, 2013 at 15:31

2 Answers 2

5

Your test connections killed by GC, if you "pin" down your connections until end of test - you'll get your 5000 connections( i've just copy/pasted and fixed/tested your code ). Rewritten connection test:

    public static void ConnectTest(int count)
    {
        var ep = new IPEndPoint(IPAddress.Loopback, port);
        List<TcpClient> clients = new List<TcpClient>();
        for (var i = 0; i < count; i++)
        {
            TcpClient cl = new TcpClient();
            clients.Add(cl);
            cl.Connect(ep);
        }
    }

Also please note that in "real world" conditions you need to add something like this before starting listener loop:

ThreadPool.SetMinThreads(100, 100);

This let your code work under "burst" connections count/network loading. We've just successfully "load tested" 7,5K connections under linux/mono.

0

You can't have 100,000 connections to a single machine. TCP/IP only has 16 bits for a port number (maximum 64k ports), and a good number of these are used (or reserved) for the OS or various services.

Normally, only 16,383 of these ports are available for incoming client connections, and you'll need to share them with all the other processes that use TCP/IP regularly.

1
  • Original question was "why async version crashes", not about 100k connections. You can test with 10k - async still crashes.
    – Savvkin
    Commented Jul 8, 2013 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.