简体   繁体   English

我可以在服务器循环中使用阻止TCP Socket.Send吗?

[英]Can I use blocking TCP Socket.Send in a server loop?

If one of the sockets blocks Send() it can affect other client(s). 如果套接字之一阻止了Send(),则可能会影响其他客户端。 I am worried about that: does Send() waits for the "delivered" answer? 我担心:Send()是否等待“已传递”的答案? If it is than a remote client can block my server events processing loop by ignoring incoming packets - then I should use BeginSend. 如果不是,那么远程客户端可以通过忽略传入数据包来阻止我的服务器事件处理循环-那么我应该使用BeginSend。 But I've measured that BeginSend eats more CPU. 但是我测量到BeginSend会消耗更多的CPU。 So I want to know weather it's acceptable to use blocking Send in the server events loop? 所以我想知道在服务器事件循环中使用阻止发送是否可以接受?

As the MSDN states 如MSDN所述

The successful completion of a send does not indicate that the data was successfully delivered. 发送成功完成并不表示数据已成功传递。

I know that socket can also block when its send buffer becomes filled. 我知道套接字也可以在其发送缓冲区被填满时阻塞。

If one of the sockets blocks Send() it can affect other client(s). 如果套接字之一阻止了Send(),则可能会影响其他客户端。

Only if your server is single-threaded. 仅当您的服务器为单线程时。 Solution: multi-threading. 解决方案:多线程。

I am worried about that: does Send() waits for the "delivered" answer? 我担心:Send()是否等待“已传递”的答案?

No, but it can block if the client isn't reading and the buffers fill up. 否,但是如果客户端不读取并且缓冲区已满,它可能会阻塞。

If it is than a remote client can block my server events processing loop by ignoring incoming packets - then I should use BeginSend. 如果不是,那么远程客户端可以通过忽略传入数据包来阻止我的服务器事件处理循环-那么我应该使用BeginSend。 But I've measured that BeginSend eats more CPU. 但是我测量到BeginSend会消耗更多的CPU。 So I want to know weather it's acceptable to use blocking Send in the server events loop? 所以我想知道在服务器事件循环中使用阻止发送是否可以接受?

No it isn't. 不,不是。 Use multi-threading, or non-blocking I/O, or Async I/O if you can overcome your aversion. 如果可以克服厌恶情绪,请使用多线程,非阻塞I / O或异步I / O。 Your suggestion of 3-4% overhead doesn't sound right to me. 您对3-4%的管理费用的建议对我来说听起来不对。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM