python 中的 TCP 套接字行为
我用 Python 进行客户端-服务器通信。如果我以一个包的形式从服务器向客户端发送 10MB 的数据,客户端可以在一个包中处理这么大的数据吗?如果客户端在 1 分钟后读取数据怎么办?例如,我建立连接并向客户端发送 10MB 消息,但客户端仅在 1 分钟或更长时间后才能接收消息。 TCP缓冲区有多大?是否有可能丢失数据(缓冲区溢出)。会不会挂服务器?
time.sleep(60)
self.request.recv( 20480 )
I have a client-server communication in Python. If I'm sending 10MB data from the server to the client in one package can the client handle this big data in one package? What if the client is reading the data after 1minute. For example, I establish a connection and sending the 10MB message to the client, but the client is capable to receive the message only after 1 minute ore more. How big is the TCP buffer? Is it possible to lose data (buffer overflow). Will it hang the server?
time.sleep(60)
self.request.recv( 20480 )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TCP是面向流的,而不是面向包的,因此在TCP中谈论“一个包”是没有意义的。虽然数据实际上将作为 IP 数据包发送到网络堆栈的下游,但 TCP 层将保证您无法辨别数据如何拆分为数据包并在接收端重新组装。
标准中没有规定TCP缓冲区大小,但如果服务器端缓冲区已满时服务器不从套接字读取数据,客户端将停止发送。换句话说,TCP 做的是流量控制。在这种情况下,您实际上可能会挂起客户端。
您应该做的是不断地从客户端发送一些可管理大小(例如 8k)的块,并不断地在服务器上读取。如果需要,数据应该缓冲在程序的服务器端。正如我所说,TCP 不处理数据包。
TCP is stream oriented, not packet oriented, so it makes no sense to talk about "one package" in TCP. While the data will in fact be sent as IP packets further down the network stack, the TCP layer will guarantee that you can not discern how the data is split into packets and reassembled on the receiving side.
The TCP buffer size is not specified in a standard, but if the server does not read from the socket when the buffer on the server side is full, the client will stop sending. In other words, TCP does flow control. In this scenario, you might actually hang the client.
What you should do is to continually send chunks of some manageable size (say 8k) from the client, and continually read on the server. The data should be buffered on the server side in your program if you need it. As I said, TCP does not deal in packets.