编程语言
首页 > 编程语言> > 在python tcp服务器和c客户端之间交谈

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

作者:互联网

我在尝试在python TCP服务器和c TCP客户端之间进行通信时遇到问题.
在第一个呼叫正常运行之后,后续的呼叫会引起问题.

就WinSock而言,send()函数正常工作,它返回正确的长度,而WSAGetLastError()不返回任何有意义的东西.

但是,当使用wireshark观察数据包时,我注意到第一个调用发送了两个数据包,其中包含所有数据的PSH,ACK,以及紧随其后的ACK,但是随后的调用(不起作用)仅发送PSH,ACK数据包,而不是后续的ACK数据包

接收方计算机的wireshark证实了这一点,并且python服务器什么也不做,它没有任何数据从套接字输出,而且我无法更深入地调试,因为套接字是本机类

当我运行一个c客户端和一个c服务器(python将执行的操作的hacked副本)时,即使在第一次调用之后,客户端也会始终如实地发送PSH,ACk和ACK数据包.

Winsock发送功能是否应该始终发送PSH,ACK和ACK?
如果是这样,为什么在连接到我的C服务器而不是python服务器时会这样做呢?
有没有人有与此类似的问题?

解决方法:

client sends a PSH,ACK and then the
server sends a PSH,ACK and a
FIN,PSH,ACK

有一个FIN,是否可能是您的Python版本的服务器在初次读取后立即关闭了连接?

如果您未明确关闭服务器的套接字,则服务器的远程套接字变量可能超出范围,因此将其关闭(并且您的C版本中不存在此错误)?

假设是这种情况,我可以使用此服务器代码生成非常类似的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)

对于客户来说:

# 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')

启动数据包嗅探器,然后运行server.py,然后运行client.py.这是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

标签:winsock,sockets,networking,python,c-4
来源: https://codeday.me/bug/20191210/2102096.html