开发者

.NET TcpSocket programming

开发者 https://www.devze.com 2023-02-25 04:55 出处:网络
What is the correct way to accept sockets in a multi connection environment in .NET? Will the following be enough even if the load is high?

What is the correct way to accept sockets in a multi connection environment in .NET? Will the following be enough even if the load is high?

while (true)
{
   //block untill socket accepted
   var socket = tcpListener.Accep开发者_Python百科tSocket();
   DoStuff(socket) //e.g. spawn thread and read data
}

That is, can I accept sockets in a single thread and then handle the sockets in a thread / dataflow / whatever. So the question is just about the accept part..


You'll probably want the BeginAccept async operation instead of the synchroneous Accept.

And if you want to handle high load, you definitely don't want a thread per connection - again, you async methods.


Take a look at either the Reactor or Proactor pattern depending on if you wan't to block or not. I'll recommend the Patterns for Concurrent and Networked Objects book.


This should be fine but if the load gets even higher you might consider using the asynchronous versions of this method: BeginAcceptSocket/EndAcceptSocket.


The BeginAcceptSocket is a better choice if you want the most performant server.

More importantly, these async operations use a Threadpool under the hood whilst in your current implementation you are creating and destroying lots of threads which is really expensive.


I think the best approach is to call BeginAccept(), and within OnAccept call BeginAccept right again.. This should give you the best concurrency.

The OnAccept should be something like this:

private void OnAccept(IAsyncResult ar)
{
   bool beginAcceptCalled = false;
   try
   {
       //start the listener again
       _listener.BeginAcceptSocket(OnAccept, null);
       beginAcceptCalled = true;
       Socket socket = _listener.EndAcceptSocket(ar);
       //do something with the socket..
   }
   catch (Exception ex)
   {
       if (!beginAcceptCalled)
       {
          //try listening to connections again
          _listener.BeginAcceptSocket(OnAccept, null);
        }
    }
}


It doesn't really matter performance wise. What matters is how you communicate which each client. That handling will consume a lot more CPU than accepting sockets.

I would use BeginAccept/EndAccept for the listener socket AND BeginReceive/EndReceive for the client sockets.


Since I'm using Async CTP and DataFlow, the current code looks like this:

private async void WaitForSockets()
{
    var socket = await tcpListener.AcceptSocketAsync();
    WaitForSockets();
    incomingSockets.Post(socket);
}

Note that what looks like a recursive call will not cause stack overflow or block. It will simply start a new awaiter for a new socket and exit.

0

精彩评论

暂无评论...
验证码 换一张
取 消