可以发送功能块
我正在编写一个聊天程序,对于服务器,当我发送数据时,send()函数是否需要很长时间才能发送数据?
这是我的问题: 我正在使用带 epoll 的 linux 2.6,单线程服务器 如果 send() 阻塞,则意味着服务器上的所有其他活动都将停止。就像如果有一个非常慢的客户端长时间不向 TCP 数据包发送 ACK 响应,发送功能会立即继续,还是会等待客户端很长时间。我不希望的是单个/几个慢速客户端会导致聊天服务器延迟。
我想要的是 send() 非阻塞并快速返回。如果它没有发送所有数据,它只会返回发送的数量,我将从缓冲区中删除它,并在下次服务时继续发送,直到发送所有数据。基本上我不想在缓慢或无响应的客户端上长时间阻塞发送。
I'm writing a chat program and for the server, when I send data can the send() function take a long time to send out the data?
Here is my problem:
I'm using linux 2.6 with epoll, server in single thread
If send() blocks, then this means all other activity on the server will stop. Like if there is a very slow client that does not send ACK responses for a long time to a tcp packet, will the send function just move on right away, or will it wait a long time for the client. The thing I don't want is for a single/few slow clients to cause delays in the chat server.
What I want is for send() to nonblock and to return very quickly. If it doesn't send all the data, it will simply return the amount sent and I will remove that from the buffer and keep sending next time serviced until all data sent. Basically I don't want send to block for a long time on a slow or unresponsive client.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将套接字设置为非阻塞模式,并且发送不会阻塞。问题是,您必须管理发生部分写入的事实,并在写入文件描述符再次激活时发送其余数据。
总的来说,我发现在非阻塞模式下执行发送和接收虽然使程序复杂化,但效果很好。
使用类似的东西:
You can set a socket to non-blocking mode and a send will not block. The problem is that you'll have to manage the fact that a partial write occurred and send the rest of the data when the write file descriptor becomes active again.
In general I've found that doing both send and recv in non-blocking mode, while complicating the program, works pretty well.
Use something like: