Server design using SocketAsyncEventArgs

后端 未结 2 1551
猫巷女王i
猫巷女王i 2021-02-01 11:09

I want to create an asynchronous socket Server using the SocketAsyncEventArgs event.

The server should manage about 1000 connections at the same time. What is the best w

2条回答
  •  不要未来只要你来
    2021-02-01 11:35

    In your code, you do this:

    if (!socket.ReceiveAsync(e.AsyncEventArgs)) {
      this.HandleIOCompleted(null, e);
    }
    

    But it is an error to do that. There is a reason why the callback is not invoked when it finishes synchronously, such action can fill up the stack.

    Imagine that each ReceiveAsync is always returning synchronously. If your HandleIOCompleted was in a while, you could process the result that returned synchronously at the same stack level. If it didn't return synchronously, you break the while. But, by doing the you you do, you end-up creating a new item in the stack... so if you have bad luck enough, you will cause stack overflow exceptions.

提交回复
热议问题