.NET Socket 监听积压

发布于 2024-10-11 10:19:39 字数 849 浏览 0 评论 0原文

我有一个服务器套接字,已将其配置为一次允许一个连接(通过使用信号量阻止 Accept 调用),积压队列大小为 1。也就是说,我调用了 .Listen(1)。

然后,我按照以下过程进行操作:

  • 我在服务器套接字上调用 AcceptAsync (仅一次)
  • 我有一个客户端 ConnectAsync (连接成功)
  • 我有一个客户端 ConnectAsync (连接成功,据说在队列中......一种告诉方式会很好)
  • 我有第三个客户端 ConnectAsync

这三个 ConnectAsync 调用快速连续发生。

第三个 ConnectAsync 的预期结果是使 SocketAsyncEventArgs“SocketError”属性不是“SocketError.Success”。我实际上希望“SocketError.ConnectionRefused”是具体的。

大约95%的时间都是这种情况。第三个客户端的回调给我一个 SocketError 值,而不是 Success。

不过,第三个 ConnectAsync 有时会“工作”,与第二个 ConnectAsync 的工作方式相同。 EventArgs.SocketError 给我 SocketError.Success,并且相应的 Socket.Connected 属性读取“true”。

这是怎么回事?我恰好一次调用 AcceptAsync(我已使用断点仔细验证了这一点),因此只应接受一个客户端,其余客户端应位于积压队列中。我的队列大小是1,那么第三个客户端时不时连接成功怎么办?

请不要告诉我使用更大的队列大小。这是我编写的测试函数,而不是积极为客户服务的代码。此时更多的是好奇。 :)

I have a server socket that I have configured to allow one connection at once (by blocking Accept calls with a semaphore), with a backlog queue size of 1. That is, I called .Listen(1).

I then follow the following process:

  • I call AcceptAsync on my server socket (only once)
  • I have a client ConnectAsync (connects successfully)
  • I have a client ConnectAsync (connects successfully, supposedly in the queue... a way to tell would be nice)
  • I have a third client ConnectAsync

These three ConnectAsync calls happen in rapid succession.

The expected result for the third ConnectAsync is to have the SocketAsyncEventArgs "SocketError" property be something other than "SocketError.Success". I actually expect "SocketError.ConnectionRefused", to be specific.

About 95% of the time, this is the case. The third client's callback gives me a SocketError value other than Success.

Every now and then, though, the third ConnectAsync "works", in the same way the second one does. The EventArgs.SocketError gives me SocketError.Success, and the corresponding Socket.Connected property reads "true".

What's going on? I call AcceptAsync exactly once (I have verified this carefully with breakpoints), so only one client should be accepted, and the rest should be on the backlog queue. My queue size is 1, so how is the third client connecting successfully every now and then?

Please don't tell me to use a larger queue size. This is for a test function I've written, and not code that is actively serving clients. At this point it's more curiosity. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铁轨上的流浪者 2024-10-18 10:19:39

那是行不通的。系统会将您的监听积压 1 提高到其自身的最小值,即至少 50。

只需将您的服务器设置为单线程即可。那么它一次只会处理一个连接。后续连接将在积压队列中等待。当积压队列已满时,如果服务器是 Windows 平台,它们将收到“连接被拒绝”的消息。不然的话。

That won't work. The system will raise your listen backlog of 1 to its own minimum which is going to be at least 50.

Just make your server single-threaded. Then it will only process one connection at a time. Subsequent connections will wait in the backlog queue. When the backlog queue is full they will get 'connection refused' if the server is a Windows platform. Not otherwise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文