TcpListener积压概念误解

时间:2019-05-15 19:24:44

标签: c# asynchronous client-server

我试图了解TcpListener类的backlog参数,但是我在如何同时达到最大未决连接数方面感到很苦恼,因此可以对其进行测试。

我有一个示例异步服务器和客户端代码。 MSDN表示,积压是挂起的连接队列的最大长度。我让服务器一直在监听连接,而客户端正在连接30次。我希望在第20个请求之后在客户端中抛出SocketException,因为待办事项设置为20。为什么它不阻止它?

我的第二个误解是,我是否真的需要将接受连接的逻辑放到新线程中,假设操作缓慢,大约需要10秒钟,例如通过TCP发送文件?目前,我将逻辑放在new Thread中,我知道这不是最好的解决方案,我应该使用ThreadPool,但问题是主要的。我通过将客户端的循环更改为1000次迭代进行了测试,如果我的逻辑不在新线程中,则在第200个连接后连接将被阻塞,这可能是因为Thread.Sleep每次将主线程的速度降低了10秒,并且主线程负责所有的接受回调。因此,基本上,我自己解释如下:如果我想使用相同的概念,则必须像以前一样将我的AcceptCallback逻辑放入一个新线程中,或者我必须在此处执行类似已接受的答案的操作:{{3} }。我说的对吗?

服务器代码:

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Server
{
    class Program
    {
        private static readonly ManualResetEvent _mre = new ManualResetEvent(false);

        static void Main(string[] args)
        {
            TcpListener listener = new TcpListener(IPAddress.Any, 80);

            try
            {
                listener.Start(20); 

                while (true)
                {
                    _mre.Reset();

                    Console.WriteLine("Waiting for a connection...");
                    listener.BeginAcceptTcpClient(new AsyncCallback(AcceptCallback), listener);

                    _mre.WaitOne();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private static void AcceptCallback(IAsyncResult ar)
        {
            _mre.Set();

            TcpListener listener = (TcpListener)ar.AsyncState;
            TcpClient client = listener.EndAcceptTcpClient(ar);

            IPAddress ip = ((IPEndPoint)client.Client.RemoteEndPoint).Address;
            Console.WriteLine($"{ip} has connected!");

            // Actually I changed it to ThreadPool
            //new Thread(() =>
            //{
            //  Console.WriteLine("Sleeping 10 seconds...");
            //  Thread.Sleep(10000);
            //  Console.WriteLine("Done");
            //}).Start();

            ThreadPool.QueueUserWorkItem(new WaitCallback((obj) =>
            {
                Console.WriteLine("Sleeping 10 seconds...");
                Thread.Sleep(10000);
                Console.WriteLine("Done");
            }));

            // Close connection
            client.Close();
        }
    }
}

客户代码:

using System;
using System.Net.Sockets;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 30; i++)
            {
                Console.WriteLine($"Connecting {i}");

                using (TcpClient client = new TcpClient()) // because once we are done, we have to close the connection with close.Close() and in this way it will be executed automatically by the using statement
                {
                    try
                    {
                        client.Connect("localhost", 80);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }

            Console.ReadKey();
        }
    }
}

编辑:由于我的第二个问题可能有点令人困惑,所以我将发布包含已发送消息的代码,问题是我应该这样保留它还是将NetworkStream放在新线程中?

服务器:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace Server
{
    class Program
    {
        private static readonly ManualResetEvent _mre = new ManualResetEvent(false);

        static void Main(string[] args)
        {
            // MSDN example: https://docs.microsoft.com/en-us/dotnet/framework/network-programming/asynchronous-server-socket-example
            // A better solution is posted here: https://stackoverflow.com/questions/2745401/tcplistener-is-queuing-connections-faster-than-i-can-clear-them
            TcpListener listener = new TcpListener(IPAddress.Any, 80);

            try
            {
                // Backlog limit is 200 for Windows 10 consumer edition
                listener.Start(5);

                while (true)
                {
                    // Set event to nonsignaled state
                    _mre.Reset();

                    Console.WriteLine("Waiting for a connection...");
                    listener.BeginAcceptTcpClient(new AsyncCallback(AcceptCallback), listener);

                    // Wait before a connection is made before continuing
                    _mre.WaitOne();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private static void AcceptCallback(IAsyncResult ar)
        {
            // Signal the main thread to continue
            _mre.Set();

            TcpListener listener = (TcpListener)ar.AsyncState;
            TcpClient client = listener.EndAcceptTcpClient(ar);

            IPAddress ip = ((IPEndPoint)client.Client.RemoteEndPoint).Address;
            Console.WriteLine($"{ip} has connected!");

            using (NetworkStream ns = client.GetStream())
            {
                byte[] bytes = Encoding.Unicode.GetBytes("test");
                ns.Write(bytes, 0, bytes.Length);
            }

            // Use this only with backlog 20 in order to test
            Thread.Sleep(5000);

            // Close connection
            client.Close();
            Console.WriteLine("Connection closed.");
        }
    }
}

客户:

using System;
using System.Net.Sockets;
using System.Text;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 33; i++)
            {
                Console.WriteLine($"Connecting {i}");

                using (TcpClient client = new TcpClient()) // once we are done, the using statement will do client.Close()
                {
                    try
                    {
                        client.Connect("localhost", 80);

                        using (NetworkStream ns = client.GetStream())
                        {
                            byte[] bytes = new byte[100];
                            int readBytes = ns.Read(bytes, 0, bytes.Length);
                            string result = Encoding.Unicode.GetString(bytes, 0, readBytes);
                            Console.WriteLine(result);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }

            Console.ReadKey();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

listen backlog is defined in RFC 6458并告知操作系统accept queue中允许的最大套接字数。

传入连接由TCP / IP堆栈放置在此队列中,并在服务器调用Accept处理新连接时将其删除。

在您的问题中,两个版本的服务器代码都从主线程循环调用Accept,并等待AcceptCallback启动之后再进行另一个接受调用。这导致非常快耗尽队列。

要演示侦听队列溢出,最简单的方法是放慢服务器的接受速度-例如减慢到零:

    var serverEp = new IPEndPoint(IPAddress.Loopback, 34567);
    var serverSocket = new TcpListener(serverEp);        
    serverSocket.Start(3);
    for (int i = 1; i <= 10; i++)
    {
        var clientSocket = new TcpClient();
        clientSocket.Connect(serverEp);
        Console.WriteLine($"Connected socket {i}");
    }   

在您的示例中,您可以只在主线程中Accept循环的末尾添加睡眠,并提高连接速率。

在现实世界中,最佳积压量取决于:

  • 客户端/互联网/操作系统可以填充队列的速率
  • 操作系统/服务器可以处理队列的速率

我不建议直接使用Thread,这是使用TaskSocket Task Extensions的服务器外观:

    static async Task Main(string[] args)
    {
        var server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        server.Bind(new IPEndPoint(IPAddress.Any, 80));
        server.Listen(5);            
        while (true)
        {
            var client = await server.AcceptAsync();
            var backTask = ProcessClient(client); 
        }  
    }

    private static async Task ProcessClient(Socket socket)
    {
        using (socket)
        {
            var ip = ((IPEndPoint)(socket.RemoteEndPoint)).Address;
            Console.WriteLine($"{ip} has connected!");

            var buffer = Encoding.Unicode.GetBytes("test");
            await socket.SendAsync(buffer, SocketFlags.None);
        }
        Console.WriteLine("Connection closed.");            
    }