TCP事务越来越长

时间:2017-08-15 13:36:29

标签: sockets networking tcp wireshark

我有一台设备,我试图通过TCP进行交谈。它非常简单,以下(python)代码几乎就是我在实践中想做的事情。

我应该发送一个数据包来请求设备更改设置,然后它应该收到一个"操作完成数据包" (这只是' 1')。

我在C中实现了下面的代码并得到了相同的结果,因此我确信它不是Python问题。

import numpy as np
import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Set some values to send to the device
values = np.arange(0.2, 1.0, 0.02)

sock.connect(('192.168.1.147', 9221))

for value in values:
    # This string sets a value, then requests an operation complete output.
    sock.sendall('I1 %1.3f; *OPC?'.encode('utf-8') % (value, ))
    print(sock.recv(32)) # Prints out '1', or b'1\r\n' in python 3

我实际经历的是输出是预期输出,但发送和接收之间的延迟逐渐变长,直到我必须终止该程序。

产生的wireshark输出很有意思:

No.     Time           Source                Destination           Protocol Length Info
      4 2.970271597    192.168.1.106         192.168.1.147         TCP      74     49938 → 9221 [SYN] Seq=0 Win=26880 Len=0 MSS=8960 SACK_PERM=1 TSval=26446953 TSecr=0 WS=128
      5 2.971102415    192.168.1.147         192.168.1.106         TCP      60     9221 → 49938 [SYN, ACK] Seq=0 Ack=1 Win=5840 Len=0 MSS=1460
      6 2.971118924    192.168.1.106         192.168.1.147         TCP      54     49938 → 9221 [ACK] Seq=1 Ack=1 Win=26880 Len=0
      7 2.971152591    192.168.1.106         192.168.1.147         TCP      69     49938 → 9221 [PSH, ACK] Seq=1 Ack=1 Win=26880 Len=15
      8 2.977589098    192.168.1.147         192.168.1.106         TCP      60     9221 → 49938 [PSH, ACK] Seq=1 Ack=1 Win=5840 Len=3
      9 2.977597172    192.168.1.106         192.168.1.147         TCP      54     49938 → 9221 [ACK] Seq=16 Ack=4 Win=26880 Len=0
     10 2.977948459    192.168.1.147         192.168.1.106         TCP      60     9221 → 49938 [ACK] Seq=1 Ack=16 Win=5840 Len=0
     11 2.977952056    192.168.1.106         192.168.1.147         TCP      54     [TCP Dup ACK 9#1] 49938 → 9221 [ACK] Seq=16 Ack=4 Win=26880 Len=0
     12 3.167593066    192.168.1.106         192.168.1.147         TCP      69     [TCP Spurious Retransmission] 49938 → 9221 [PSH, ACK] Seq=1 Ack=4 Win=26880 Len=15
     13 3.168475846    192.168.1.147         192.168.1.106         TCP      60     [TCP Dup ACK 10#1] 9221 → 49938 [ACK] Seq=4 Ack=16 Win=5840 Len=0
     14 3.168487149    192.168.1.106         192.168.1.147         TCP      69     49938 → 9221 [PSH, ACK] Seq=16 Ack=4 Win=26880 Len=15
     15 3.174457755    192.168.1.147         192.168.1.106         TCP      60     9221 → 49938 [PSH, ACK] Seq=4 Ack=16 Win=5840 Len=3
     16 3.174481722    192.168.1.106         192.168.1.147         TCP      54     49938 → 9221 [ACK] Seq=31 Ack=7 Win=26880 Len=0
     17 3.174817948    192.168.1.147         192.168.1.106         TCP      60     9221 → 49938 [ACK] Seq=4 Ack=31 Win=5840 Len=0
     18 3.567587105    192.168.1.106         192.168.1.147         TCP      69     [TCP Spurious Retransmission] 49938 → 9221 [PSH, ACK] Seq=16 Ack=7 Win=26880 Len=15
     19 3.568540028    192.168.1.147         192.168.1.106         TCP      60     [TCP Dup ACK 17#1] 9221 → 49938 [ACK] Seq=7 Ack=31 Win=5840 Len=0
     20 3.568551611    192.168.1.106         192.168.1.147         TCP      69     49938 → 9221 [PSH, ACK] Seq=31 Ack=7 Win=26880 Len=15
     21 3.574509787    192.168.1.147         192.168.1.106         TCP      60     9221 → 49938 [PSH, ACK] Seq=7 Ack=31 Win=5840 Len=3
     22 3.574533527    192.168.1.106         192.168.1.147         TCP      54     49938 → 9221 [ACK] Seq=46 Ack=10 Win=26880 Len=0
     23 3.574857577    192.168.1.147         192.168.1.106         TCP      60     9221 → 49938 [ACK] Seq=7 Ack=46 Win=5840 Len=0
     24 3.574870866    192.168.1.106         192.168.1.147         TCP      54     [TCP Dup ACK 22#1] 49938 → 9221 [ACK] Seq=46 Ack=10 Win=26880 Len=0
     25 4.367591502    192.168.1.106         192.168.1.147         TCP      69     [TCP Spurious Retransmission] 49938 → 9221 [PSH, ACK] Seq=31 Ack=10 Win=26880 Len=15
     26 4.368487116    192.168.1.147         192.168.1.106         TCP      60     [TCP Dup ACK 23#1] 9221 → 49938 [ACK] Seq=10 Ack=46 Win=5840 Len=0
     27 4.368498284    192.168.1.106         192.168.1.147         TCP      69     49938 → 9221 [PSH, ACK] Seq=46 Ack=10 Win=26880 Len=15
     28 4.374526599    192.168.1.147         192.168.1.106         TCP      60     9221 → 49938 [PSH, ACK] Seq=10 Ack=46 Win=5840 Len=3
     29 4.374558188    192.168.1.106         192.168.1.147         TCP      54     49938 → 9221 [ACK] Seq=61 Ack=13 Win=26880 Len=0
     30 4.374881659    192.168.1.147         192.168.1.106         TCP      60     9221 → 49938 [ACK] Seq=10 Ack=61 Win=5840 Len=0
     31 4.374896303    192.168.1.106         192.168.1.147         TCP      54     [TCP Dup ACK 29#1] 49938 → 9221 [ACK] Seq=61 Ack=13 Win=26880 Len=0
     32 5.971603454    192.168.1.106         192.168.1.147         TCP      69     [TCP Spurious Retransmission] 49938 → 9221 [PSH, ACK] Seq=46 Ack=13 Win=26880 Len=15
     33 5.972478351    192.168.1.147         192.168.1.106         TCP      60     [TCP Dup ACK 30#1] 9221 → 49938 [ACK] Seq=13 Ack=61 Win=5840 Len=0
     34 5.972490012    192.168.1.106         192.168.1.147         TCP      69     49938 → 9221 [PSH, ACK] Seq=61 Ack=13 Win=26880 Len=15
     35 5.978397699    192.168.1.147         192.168.1.106         TCP      60     9221 → 49938 [PSH, ACK] Seq=13 Ack=61 Win=5840 Len=3
     36 5.978418493    192.168.1.106         192.168.1.147         TCP      54     49938 → 9221 [ACK] Seq=76 Ack=16 Win=26880 Len=0
     37 5.978754841    192.168.1.147         192.168.1.106         TCP      60     9221 → 49938 [ACK] Seq=13 Ack=76 Win=5840 Len=0
     38 5.978768499    192.168.1.106         192.168.1.147         TCP      54     [TCP Dup ACK 36#1] 49938 → 9221 [ACK] Seq=76 Ack=16 Win=26880 Len=0
     43 7.134413907    192.168.1.106         192.168.1.147         TCP      69     49938 → 9221 [FIN, PSH, ACK] Seq=76 Ack=16 Win=26880 Len=15
     44 7.140478879    192.168.1.147         192.168.1.106         TCP      60     9221 → 49938 [PSH, ACK] Seq=16 Ack=76 Win=5840 Len=3
     45 7.140515226    192.168.1.106         192.168.1.147         TCP      54     49938 → 9221 [RST] Seq=76 Win=0 Len=0
     46 7.140822269    192.168.1.147         192.168.1.106         TCP      60     9221 → 49938 [ACK] Seq=16 Ack=91 Win=5840 Len=0
     47 7.140838249    192.168.1.106         192.168.1.147         TCP      54     49938 → 9221 [RST] Seq=91 Win=0 Len=0

(此时我杀了它)

它向我建议的是ACK正在发送,但由于某种原因被忽略(在某种程度上是畸形的?)。然后再次发送原始数据包,延迟时间越来越长(我怀疑这是丢失数据包的正确行为),这会导致传输缓冲区被备份,因此不会发送新数据包。 Wireshark似乎认为重发是虚假的,正如#34; Spurious Retransmission"所示。

设备在第一次发送时就会响应数据包,我可以从它的显示屏上看到。

可能存在一些协议不正确,但我不知道如何诊断问题。我很乐意为此工作 - 在这个阶段它不是生产。

(仅供参考,该设备是TTi电源)。

1 个答案:

答案 0 :(得分:0)

事实证明问题是硬件上的固件问题。我很好奇ACK数据包中的错误导致重新发送,但就我而言,问题已解决。

为了完整性和任何遇到相同问题的人,电源是TTi PL303QMD。我从固件版本3.02升级到4.06,问题解决了。