python立即发送TCP/IP数据 [英] python sending TCP/IP data instantly

查看:73
本文介绍了python立即发送TCP/IP数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用类似这样的东西在我的服务器中通过 TCP/IP 发送数据

I am currently sending data over TCP/IP in myserver using something like this

for str in lst:
  data = str + "\n"
  self._conn.sendall(data)

现在假设我的列表中有以下两个字符串

Now suppose my list has the following two string in it

1-This is statement 1 in list
2-This is statement 2 in list

我的客户正在接收这样的第 2 行的一半.

My client is receiving half of line 2 like this.

   This is statement 1 in list
   This is 

我想分别发送列表中的第 1 行和第 2 行.我知道 TCP/IP 以这种方式工作,它将发送可发送的全部数据.我想我可以在调用 self._conn.sendall(data) 后延迟,但我想知道我还有哪些其他选择.我无法对数据的接收方进行更改,而只能对发送方进行更改.到目前为止,我唯一的选择是在每次发送后添加延迟.

I would like to send line1 and then line 2 in the list individually. I understand that TCP/IP works this way in which it will send the entire data that is available to send. I think I could put a delay in after calling self._conn.sendall(data) but i wanted to know what other options I have. I cannot make changes to the receiver of the data and I can only make changes to the sender. So far my only option is adding a delay after each send.

推荐答案

TCP 处理数据流,而不是单个数据包.这就像从文件中读取数据.发送方将数据放入其发送缓冲区,TCP 可以自行决定何时发送.到达接收应用程序的时间取决于数据的发送时间和(通常是不可预测的)网络状况.

TCP works with streams of data, not individual packets. It's like reading data from a file. The sender puts data in its send buffer, and TCP can decide for itself when to send it. The timing of the arrival at the receiving application depends on when the data was sent and on (often unpredictable) network conditions.

如果您使用 TCP_NODELAY 标志在您的套接字中(类似于 socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1).这会导致 TCP 在数据到达时立即发送出去它的缓冲区.但是,仍然无法保证到达时间.这就是为什么任何基于时间的解决方案都会失败的原因,至少在某些情况下是这样.

TCP deliveries can be made more predicable if you use the TCP_NODELAY flag in your socket (something like socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1). This would cause TCP to send out data as soon as it arrives in its buffer. But still, there would be no guarantees as to arrival times. This is why any time based solution would break, at least in some cases.

解决办法是自己把数据流分成块.有几种方法可以做到这一点.以下是一些:

The solution is to divide the data stream into chunks yourself. There are several ways of doing that. Here are a few:

  1. 使用固定长度的消息 - 如果所有消息的长度都是固定的,接收者只需recv()正确的字节数,处理消息,然后等待相同的数字字节.

  1. Use fixed length messages - if all messages have a fixed length, the receiver just has to recv() the right number of bytes, process the message, then wait for the same number of bytes.

在每条消息之前发送消息的长度.如果您想发送字符串blah",请将其编码为0004blah"或类似的内容.接收器将(总是)读取前四个字节(即 0004)以确定要读取的剩余字节数.然后它将读取所需的字节数,处理消息,然后等待下一个.这是一个强大且易于实施的解决方案.

Send the length of the message before each message. If you want to send the string "blah", encode it as "0004blah" or something similar. The receiver will (always) read the first four bytes (which are 0004) to figure out the number of remaining bytes to read. It will then read the required number of bytes, process the message, and then wait for the next one. It's a robust solution that's also easy to implement.

使用分隔符.文本文件中的行由换行符 (\n) 分隔.同样,您可以在消息之间添加一个特殊的分隔符字节(或多个字节).例如,您可以定义消息始终以美元符号 ($) 结尾.然后接收器要做的就是从套接字逐字节读取,直到它收到一个美元符号.当然,如果采用这种方法,则必须确保消息正文中不包含分隔符.

Use a delimiter. Lines in text files are divided by newline characters (\n). Similarly, you can add a special delimiter byte (or bytes) between messages. For example, you can define that messages always end with a dollar sign ($). Then all the receiver has to do is read from the socket byte by byte until it receives a dollar sign. Of course if you take this approach, you have to make sure that the body of the messages doesn't contain the delimiter character.

这篇关于python立即发送TCP/IP数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆