Python socket recv 正在拆分收到的消息
Python socket recv is splitting received message
我正在 Python 和 Java 进程之间进行套接字通信。我正在尝试使用 java 发送一个 int 并在 Python.
中接收它
Java 方(发件人):
ServerSocket ss = new ServerSocket(6666);
Socket s = ss.accept();
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
final int numberToSend = 512;
dos.writeInt(numberToSend); //In big endian
dos.flush();
Python 方(接收方):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 6666))
while True:
data_received = s.recv(4)
int_received = int.from_bytes(data_received, byteorder='big')
print("Received:", data_received, "Number:", int_received)
do_other_stuff(int_received)
我很确定 Java 端工作正常。 dos.size()
给出 4 个字节。
但是,从 Python 的角度来看,data_received
似乎被分成两部分。对于每个周期,打印给出:
Received: b'\x00' Number 0
Received: b'\x00\x02\x00' Number 512
预期为 Received: b'\x00\x00\x02\x00' Number 512
。随着消息拆分,它会干扰 do_other_stuff
。
sys.getsizeof(data_received)
分别为第一部分和第二部分给出 18 和 20。
我也试过 int_received = struct.unpack("!i", data_received)[0]
但它给出了 struct.error: unpack requires a buffer of 4 bytes
,因为消息有 1 个和 3 个字节。
我做错了什么?
我猜你没有做任何逻辑上的错误,但这似乎是 TCP 的工作方式。在documentation about using sockets中,虽然需要一定数量,但接收处于循环等待获取预期数据量的状态。
尝试做同样的事情。 IE。在伪代码中:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 6666))
data_received = b""
long_received = 0
while True:
while long_received < 4:
data_received += s.recv(4-long_received)
long_received = len(data_received)
int_received = int.from_bytes(data_received, byteorder='big')
print("Received:", data_received, "Number:", int_received)
do_other_stuff(int_received)
data_received = b""
long_received = 0
编辑:添加了 Remy Lebeau 的注释。谢谢。
编辑 2:正如用户 user207421 所说,这是 TCP 的行为,而不是 Python。谢谢,对我来说绝对是新的。 :D
我正在 Python 和 Java 进程之间进行套接字通信。我正在尝试使用 java 发送一个 int 并在 Python.
中接收它Java 方(发件人):
ServerSocket ss = new ServerSocket(6666);
Socket s = ss.accept();
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
final int numberToSend = 512;
dos.writeInt(numberToSend); //In big endian
dos.flush();
Python 方(接收方):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 6666))
while True:
data_received = s.recv(4)
int_received = int.from_bytes(data_received, byteorder='big')
print("Received:", data_received, "Number:", int_received)
do_other_stuff(int_received)
我很确定 Java 端工作正常。 dos.size()
给出 4 个字节。
但是,从 Python 的角度来看,data_received
似乎被分成两部分。对于每个周期,打印给出:
Received: b'\x00' Number 0
Received: b'\x00\x02\x00' Number 512
预期为 Received: b'\x00\x00\x02\x00' Number 512
。随着消息拆分,它会干扰 do_other_stuff
。
sys.getsizeof(data_received)
分别为第一部分和第二部分给出 18 和 20。
我也试过 int_received = struct.unpack("!i", data_received)[0]
但它给出了 struct.error: unpack requires a buffer of 4 bytes
,因为消息有 1 个和 3 个字节。
我做错了什么?
我猜你没有做任何逻辑上的错误,但这似乎是 TCP 的工作方式。在documentation about using sockets中,虽然需要一定数量,但接收处于循环等待获取预期数据量的状态。
尝试做同样的事情。 IE。在伪代码中:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 6666))
data_received = b""
long_received = 0
while True:
while long_received < 4:
data_received += s.recv(4-long_received)
long_received = len(data_received)
int_received = int.from_bytes(data_received, byteorder='big')
print("Received:", data_received, "Number:", int_received)
do_other_stuff(int_received)
data_received = b""
long_received = 0
编辑:添加了 Remy Lebeau 的注释。谢谢。
编辑 2:正如用户 user207421 所说,这是 TCP 的行为,而不是 Python。谢谢,对我来说绝对是新的。 :D