了解TCP侦听backlog

时间:2014-01-08 09:10:03

标签: python sockets tcp handshake socketserver

我将深入了解更改TCP连接队列大小的影响。所以我写了一个简单的服务器/客户端对来测试它。

服务器在python模块ForkingTCPServer中实例化SocketServer。为了测试挂起的连接队列大小的影响,我按如下方式覆盖服务器的两个参数:

max_children = 1        # default 40, max number of child processes
request_queue_size = 0  # default 5, socket listen() backlog

下面是服务器的请求处理程序,它提供了一个简单的“延迟”回显服务。

class RequestHandler(socketserver.StreamRequestHandler):
    def handle(self):
        self.data = self.rfile.readline().strip()
        time.sleep(30)
        self.wfile.write(self.data)

在客户端,10个套接字连接服务器并在同一时间发送一行。

for i in xrange(10): 
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sockets.append(s)
    s.connect_ex(address)
    s.sendall('{}\n'.format(i)

结果只有3-4个插座成功连接。其他人甚至没有完成3次握手,最后服务器向他们发送了RST。这是其中一个失败客户端的tcpdump输出。可以看出,服务器(端口:10086)从客户端(端口:53241)收到SYN并发送回SYN/ACK。客户端在收到ACK时也发送了SYN/ACK。但是,似乎服务器没有从客户端收到ACK。所以它不断向客户端发送SYN/ACK,最后通过发送RST放弃了。

有人可以向我解释为什么服务器没有看到ACK吗?是因为连接队列溢出了吗?

17:41:30.568077 IP localhost.localdomain.53241 > localhost.localdomain.10086: Flags [S], seq 3931554775, win 32792, options [mss 16396,sackOK,TS val 2434238 ecr 0,nop,wscale 6], length 0
17:41:30.568090 IP localhost.localdomain.10086 > localhost.localdomain.53241: Flags [S.], seq 2946632838, ack 3931554776, win 32768, options [mss 16396,sackOK,TS val 2434238 ecr 2434238,nop,wscale 6], length 0
17:41:30.568100 IP localhost.localdomain.53241 > localhost.localdomain.10086: Flags [.], ack 1, win 513, options [nop,nop,TS val 2434238 ecr 2434238], length 0
17:41:30.568479 IP localhost.localdomain.53241 > localhost.localdomain.10086: Flags [P.], seq 1:3, ack 1, win 513, options [nop,nop,TS val 2434239 ecr 2434238], length 2
17:41:30.770902 IP localhost.localdomain.53241 > localhost.localdomain.10086: Flags [P.], seq 1:3, ack 1, win 513, options [nop,nop,TS val 2434440 ecr 2434238], length 2
17:41:31.172453 IP localhost.localdomain.53241 > localhost.localdomain.10086: Flags [P.], seq 1:3, ack 1, win 513, options [nop,nop,TS val 2434843 ecr 2434238], length 2
17:41:31.767360 IP localhost.localdomain.10086 > localhost.localdomain.53241: Flags [S.], seq 2946632838, ack 3931554776, win 32768, options [mss 16396,sackOK,TS val 2435438 ecr 2434843,nop,wscale 6], length 0
17:41:31.767391 IP localhost.localdomain.53241 > localhost.localdomain.10086: Flags [.], ack 1, win 513, options [nop,nop,TS val 2435438 ecr 2434238], length 0
17:41:31.976349 IP localhost.localdomain.53241 > localhost.localdomain.10086: Flags [P.], seq 1:3, ack 1, win 513, options [nop,nop,TS val 2435647 ecr 2434238], length 2
17:41:33.584352 IP localhost.localdomain.53241 > localhost.localdomain.10086: Flags [P.], seq 1:3, ack 1, win 513, options [nop,nop,TS val 2437255 ecr 2434238], length 2
17:41:33.969825 IP localhost.localdomain.10086 > localhost.localdomain.53241: Flags [S.], seq 2946632838, ack 3931554776, win 32768, options [mss 16396,sackOK,TS val 2437640 ecr 2437255,nop,wscale 6], length 0
17:41:33.969854 IP localhost.localdomain.53241 > localhost.localdomain.10086: Flags [.], ack 1, win 513, options [nop,nop,TS val 2437640 ecr 2434238], length 0
17:41:36.800489 IP localhost.localdomain.53241 > localhost.localdomain.10086: Flags [P.], seq 1:3, ack 1, win 513, options [nop,nop,TS val 2440471 ecr 2434238], length 2
17:41:38.177461 IP localhost.localdomain.10086 > localhost.localdomain.53241: Flags [S.], seq 2946632838, ack 3931554776, win 32768, options [mss 16396,sackOK,TS val 2441848 ecr 2440471,nop,wscale 6], length 0
17:41:38.177493 IP localhost.localdomain.53241 > localhost.localdomain.10086: Flags [.], ack 1, win 513, options [nop,nop,TS val 2441848 ecr 2434238], length 0
17:41:43.232468 IP localhost.localdomain.53241 > localhost.localdomain.10086: Flags [P.], seq 1:3, ack 1, win 513, options [nop,nop,TS val 2446903 ecr 2434238], length 2
17:41:46.193321 IP localhost.localdomain.10086 > localhost.localdomain.53241: Flags [S.], seq 2946632838, ack 3931554776, win 32768, options [mss 16396,sackOK,TS val 2449864 ecr 2446903,nop,wscale 6], length 0
17:41:46.193352 IP localhost.localdomain.53241 > localhost.localdomain.10086: Flags [.], ack 1, win 513, options [nop,nop,TS val 2449864 ecr 2434238], length 0
17:41:56.096467 IP localhost.localdomain.53241 > localhost.localdomain.10086: Flags [P.], seq 1:3, ack 1, win 513, options [nop,nop,TS val 2459767 ecr 2434238], length 2
17:42:02.211676 IP localhost.localdomain.10086 > localhost.localdomain.53241: Flags [S.], seq 2946632838, ack 3931554776, win 32768, options [mss 16396,sackOK,TS val 2465882 ecr 2459767,nop,wscale 6], length 0
17:42:02.211698 IP localhost.localdomain.53241 > localhost.localdomain.10086: Flags [.], ack 1, win 513, options [nop,nop,TS val 2465882 ecr 2434238], length 0
17:42:21.825117 IP localhost.localdomain.53241 > localhost.localdomain.10086: Flags [P.], seq 1:3, ack 1, win 513, options [nop,nop,TS val 2485495 ecr 2434238], length 2
17:43:13.281525 IP localhost.localdomain.53241 > localhost.localdomain.10086: Flags [P.], seq 1:3, ack 1, win 513, options [nop,nop,TS val 2536952 ecr 2434238], length 2
17:43:13.281625 IP localhost.localdomain.10086 > localhost.localdomain.53241: Flags [R], seq 2946632839, win 0, length 0

1 个答案:

答案 0 :(得分:0)

  

是因为连接队列溢出了吗?

没有。如果积压队列已满,它将不会发送SYN / ACK,它将不执行任何操作或发送RST,具体取决于平台。