Use of Socket.BeginAccept/EndAccept for multiple connections

前端 未结 2 994
甜味超标
甜味超标 2021-01-19 07:57

Unlike the synchronous Accept, BeginAccept doesn\'t provide a socket for the newly created connection. EndAccept however does, but it

相关标签:
2条回答
  • 2021-01-19 08:20

    This is normal when you deal with callback-based async IO. And it is what makes it so awful to use!

    Can you use C# await? That would simplify this to a simple while (true) { await accept(); } loop.

    0 讨论(0)
  • 2021-01-19 08:29

    The way are doing this is correct for using asynchronous sockets. Personally, I would move your BeginAccept to right after you get the socket from the AsyncState. This will allow you to accept additional connections right away. As it is right now, the handling code will run before you are ready to accept another connection.

    As Usr mentioned, I believe you could re-write the code to use await with tasks.

    0 讨论(0)
提交回复
热议问题