简体   繁体   English

在python tcp服务器和c ++客户端之间进行交谈

[英]talking between python tcp server and a c++ client

I am having an issue trying to communicate between a python TCP server and a c++ TCP client. 我在尝试在python TCP服务器和c ++ TCP客户端之间进行通信时遇到问题。 After the first call, which works fine, the subsequent calls cause issues. 在第一次调用之后,后续调用会导致问题。

As far as WinSock is concerned, the send() function worked properly, it returns the proper length and WSAGetLastError() does not return anything of significance. 就WinSock而言,send()函数正常工作,它返回正确的长度,WSAGetLastError()不返回任何重要的东西。

However, when watching the packets using wireshark, i notice that the first call sends two packets, a PSH,ACK with all of the data in it, and an ACK right after, but the subsequent calls, which don't work, only send the PSH,ACK packet, and not a subsequent ACK packet 但是,当使用wireshark观察数据包时,我注意到第一个呼叫发送了两个数据包,一个PSH,包含所有数据的ACK,以及紧接着的ACK,但后续调用不起作用,只发送PSH,ACK数据包,而不是后续的ACK数据包

the receiving computers wireshark corroborates this, and the python server does nothing, it doesnt have any data coming out of the socket, and i cannot debug deeper, since socket is a native class 接收计算机wireshark证实这一点,并且python服务器什么都不做,它没有任何数据从套接字出来,我无法调试更深,因为套接字是本机类

when i run a c++ client and a c++ server (a hacked replica of what the python one would do), the client faithfully sends both the PSH,ACk and ACK packets the whole time, even after the first call. 当我运行一个c ++客户端和一个c ++服务器(一个被python一个人会做的黑客复制品)时,即使在第一次调用之后,客户端也会忠实地发送PSH,ACk和ACK数据包。

Is the winsock send function supposed to always send a PSH,ACK and an ACK? winsock发送函数应该始终发送PSH,ACK和ACK吗? If so, why would it do so when connected to my C++ server and not the python server? 如果是这样,为什么它连接到我的C ++服务器而不是python服务器时会这样做? Has anyone had any issues similar to this? 有没有人有类似的问题?

client sends a PSH,ACK and then the server sends a PSH,ACK and a FIN,PSH,ACK 客户端发送PSH,ACK然后服务器发送PSH,ACK和FIN,PSH,ACK

There is a FIN, so could it be that the Python version of your server is closing the connection immediately after the initial read? 有一个FIN,所以你的服务器的Python版本可能在初始读取后立即关闭连接吗?

If you are not explicitly closing the server's socket, it's probable that the server's remote socket variable is going out of scope, thus closing it (and that this bug is not present in your C++ version)? 如果您没有明确关闭服务器的套接字,那么服务器的远程套接字变量可能超出范围,从而关闭它(并且您的C ++版本中不存在此错误)?

Assuming that this is the case, I can cause a very similar TCP sequence with this code for the server: 假设是这种情况,我可以使用此代码为服务器生成一个非常相似的TCP序列:

# server.py
import socket
from time import sleep

def f(s):
        r,a = s.accept()
        print r.recv(100)

s = socket.socket()
s.bind(('localhost',1234))
s.listen(1)

f(s)
# wait around a bit for the client to send it's second packet
sleep(10)

and this for the client: 这对客户来说:

# client.py
import socket
from time import sleep

s = socket.socket()
s.connect(('localhost',1234))

s.send('hello 1')
# wait around for a while so that the socket in server.py goes out of scope
sleep(5)
s.send('hello 2')

Start your packet sniffer, then run server.py and then, client.py. 启动数据包嗅探器,然后运行server.py,然后运行client.py。 Here is the outout of tcpdump -A -i lo , which matches your observations: 以下是tcpdump -A -i lo ,它与您的观察结果相符:

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 96 bytes
12:42:37.683710 IP localhost:33491 > localhost.1234: S 1129726741:1129726741(0) win 32792 <mss 16396,sackOK,timestamp 640881101 0,nop,wscale 7>
E..<R.@.@...............CVC.........I|....@....
&3..........
12:42:37.684049 IP localhost.1234 > localhost:33491: S 1128039653:1128039653(0) ack 1129726742 win 32768 <mss 16396,sackOK,timestamp 640881101 640881101,nop,wscale 7>
E..<..@.@.<.............C<..CVC.....Ia....@....
&3..&3......
12:42:37.684087 IP localhost:33491 > localhost.1234: . ack 1 win 257 <nop,nop,timestamp 640881102 640881101>
E..4R.@.@...............CVC.C<......1......
&3..&3..
12:42:37.684220 IP localhost:33491 > localhost.1234: P 1:8(7) ack 1 win 257 <nop,nop,timestamp 640881102 640881101>
E..;R.@.@...............CVC.C<......./.....
&3..&3..hello 1
12:42:37.684271 IP localhost.1234 > localhost:33491: . ack 8 win 256 <nop,nop,timestamp 640881102 640881102>
E..4.(@.@...............C<..CVC.....1}.....
&3..&3..
12:42:37.684755 IP localhost.1234 > localhost:33491: F 1:1(0) ack 8 win 256 <nop,nop,timestamp 640881103 640881102>
E..4.)@.@...............C<..CVC.....1{.....
&3..&3..
12:42:37.685639 IP localhost:33491 > localhost.1234: . ack 2 win 257 <nop,nop,timestamp 640881104 640881103>
E..4R.@.@...............CVC.C<......1x.....
&3..&3..
12:42:42.683367 IP localhost:33491 > localhost.1234: P 8:15(7) ack 2 win 257 <nop,nop,timestamp 640886103 640881103>
E..;R.@.@...............CVC.C<......./.....
&3%W&3..hello 2
12:42:42.683401 IP localhost.1234 > localhost:33491: R 1128039655:1128039655(0) win 0
E..(..@.@.<.............C<......P...b...

9 packets captured
27 packets received by filter
0 packets dropped by kernel

What size of packets do you send? 你发送的数据包大小是多少?

If they are small - may be Nagle's Algorith & Delayed ACK Algorithm is your headache? 如果它们很小 - 可能是Nagle的算法和延迟ACK算法是你的头疼吗? From what you described think Delayed ACK is involved... 根据您的描述,我们认为延迟确认涉及......

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

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