18.11.4 呼入连接请求队列
一个并发服务器调用一个新的进程来处理每个客户请求,
因此处于被动连接请求的服务器应该始终准备处理下一个呼入的连接请求
1) 正等待连接请求的一端有一个固定长度的连接队列,
该队列中的连接已被TCP接受(即三次握手已经完成),但还没有被应用层所接受
2) 应用层将指明该队列的最大长度,这个值通常称为积压值(backlog).
它的取值范围是0~5之间的整数,包括0和5
3)
socket.listen(backlog)
Listen for connections made to the socket. The backlog argument specifies the maximum number of queued connections and should be at least 0;
the maximum value is system-dependent (usually 5), the minimum value is forced to 0.
侦听用于socket的连接, backlog 参数指定了 排队连接的最大数量
-l, --listening
Display listening sockets.
-n, --numeric
Do now try to resolve service names.
Display only TCP sockets.
客户端:
C:UsersTLCB>netstat -na | findstr 5555
TCP 192.168.137.1:58164 192.168.137.3:5555 ESTABLISHED
TCP 192.168.137.1:58178 192.168.137.3:5555 ESTABLISHED
TCP 192.168.137.1:58304 192.168.137.3:5555 ESTABLISHED
TCP 192.168.137.1:58355 192.168.137.3:5555 ESTABLISHED
TCP 192.168.137.1:58474 192.168.137.3:5555 ESTABLISHED
服务端:
node2:/root#netstat -na | grep 5555
tcp 0 0 0.0.0.0:5555 0.0.0.0:* LISTEN
tcp 0 0 192.168.137.3:5555 192.168.137.1:59794 SYN_RECV
tcp 3 0 192.168.137.3:5555 192.168.137.1:58304 ESTABLISHED
tcp 26 0 192.168.137.3:5555 192.168.137.1:58178 ESTABLISHED
tcp 0 0 192.168.137.3:5555 192.168.137.1:58164 ESTABLISHED
node2:/root#ss -lnt | grep :5555
LISTEN 2 1 *:5555 *:*
注意区分T C P接受一个连接是将其放入这个队列,而应用层接受连接是将其从该队列
中移出
如果对于新的连接请求,连接队列中已没有空间, T C P将不理会收到的 S Y N。也不发
回任何报文段(即不发回 R S T)。如果应用层不能及时接受已被 T C P接受的连接,这些
连接可能占满整个连接队列,客户的主动打开最终将超时。
通过sock程序能了解这种情况。我们调用它,并使用新的选项(-o).
让它在创建一个新的服务器进程后没有接受任何连接请求之前暂停下来。
如果在它暂停期间又调用了多个客户进程,它将导致接受连接队列被填满,通过tcpdump能够看到这一切
sock -s -v -q1 -O30 5555
-O n #ms to pause after listen, but before first accept
在接受连接前 暂停下来
-q n size of listen queue for TCP server (default 5)
侦听队列大小 默认是5
-q1 选项将服务器端的积压值置1.在这种情况下,
传统的BSD系统中的队列允许接受两个连接请求。
-O30 选项使程序接受任何客户连接前暂停30秒。 在这个30秒内,
我们可以启动其他客户端进程来填充这个对列。
tcpdump -S -i eth1 '((tcp) and (port 5555) and (host 192.168.137.1))'
node2:/root#sock -s -v -q1 -O3000 5555
第一个连接:
C:UsersTLCB>netstat -na | findstr 5555
TCP 192.168.137.1:54800 192.168.137.3:5555 ESTABLISHED
node2:/root#netstat -na | grep 5555
tcp 0 0 0.0.0.0:5555 0.0.0.0:* LISTEN
tcp 0 0 192.168.137.3:5555 192.168.137.1:54800 ESTABLISHED
node2:/root#ss -lnt | grep :5555
LISTEN 0 1 *:5555 *:*
node2:/root#
第一个连接完成3次握手;
node2:/root# tcpdump -S -i eth1 '((tcp) and (port 5555) and (host 192.168.137.1))'
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth1, link-type EN10MB (Ethernet), capture size 65535 bytes
02:53:36.068169 IP 192.168.137.1.54800 > node2.personal-agent: Flags [S], seq 3223484620, win 8192, options [mss 1460,nop,wscale 2,nop,nop,sackOK], length 0
02:53:36.068193 IP node2.personal-agent > 192.168.137.1.54800: Flags [S.], seq 3206251611, ack 3223484621, win 2560, options [mss 256,nop,nop,sackOK,nop,wscale 7], length 0
02:53:36.068386 IP 192.168.137.1.54800 > node2.personal-agent: Flags [.], ack 3206251612, win 16482, length 0
02:53:36.068612 IP node2.personal-agent > 192.168.137.1.54800: Flags [P.], seq 3206251612:3206251613, ack 3223484621, win 20, length 1
02:53:36.267891 IP 192.168.137.1.54800 > node2.personal-agent: Flags [.], ack 3206251613, win 16481, length 0
第2个连接:
TCP 192.168.137.1:54904 192.168.137.3:5555 ESTABLISHED
node2:/root#netstat -na | grep 5555
tcp 0 0 0.0.0.0:5555 0.0.0.0:* LISTEN
tcp 0 0 192.168.137.3:5555 192.168.137.1:54904 ESTABLISHED
tcp 0 0 192.168.137.3:5555 192.168.137.1:54800 ESTABLISHED
node2:/root#ss -lnt | grep :5555
LISTEN 1 1 *:5555 *:*
02:55:20.756753 IP 192.168.137.1.54904 > node2.personal-agent: Flags [S], seq 1913127822, win 8192, options [mss 1460,nop,wscale 2,nop,nop,sackOK], length 0
02:55:20.756790 IP node2.personal-agent > 192.168.137.1.54904: Flags [S.], seq 3421878813, ack 1913127823, win 2560, options [mss 256,nop,nop,sackOK,nop,wscale 7], length 0
02:55:20.756957 IP 192.168.137.1.54904 > node2.personal-agent: Flags [.], ack 3421878814, win 16482, length 0
第三个连接:
TCP 192.168.137.1:54936 192.168.137.3:5555 ESTABLISHED
node2:/root#netstat -na | grep 5555
tcp 0 0 0.0.0.0:5555 0.0.0.0:* LISTEN
tcp 0 0 192.168.137.3:5555 192.168.137.1:54936 ESTABLISHED
tcp 0 0 192.168.137.3:5555 192.168.137.1:54904 ESTABLISHED
tcp 0 0 192.168.137.3:5555 192.168.137.1:54800 ESTABLISHED
node2:/root#ss -lnt | grep :5555
LISTEN 2 1 *:5555 *:*
02:56:49.176804 IP 192.168.137.1.54936 > node2.personal-agent: Flags [S], seq 746356564, win 8192, options [mss 1460,nop,wscale 2,nop,nop,sackOK], length 0
02:56:49.176825 IP node2.personal-agent > 192.168.137.1.54936: Flags [S.], seq 4222867080, ack 746356565, win 2560, options [mss 256,nop,nop,sackOK,nop,wscale 7], length 0
02:56:49.177068 IP 192.168.137.1.54936 > node2.personal-agent: Flags [.], ack 4222867081, win 16482, length 0
三次握手也已经完成
第四个连接:
TCP 192.168.137.1:54964 192.168.137.3:5555 ESTABLISHED
node2:/root#netstat -na | grep 5555
tcp 0 0 0.0.0.0:5555 0.0.0.0:* LISTEN
tcp 0 0 192.168.137.3:5555 192.168.137.1:54964 SYN_RECV
tcp 0 0 192.168.137.3:5555 192.168.137.1:54936 ESTABLISHED
tcp 0 0 192.168.137.3:5555 192.168.137.1:54904 ESTABLISHED
tcp 0 0 192.168.137.3:5555 192.168.137.1:54800 ESTABLISHED
node2:/root#ss -lnt | grep :5555
State Recv-Q Send-Q
LISTEN 2 1 *:5555 *:*
02:58:03.300241 IP 192.168.137.1.54964 > node2.personal-agent: Flags [S], seq 238464746, win 8192, options [mss 1460,nop,wscale 2,nop,nop,sackOK], length 0
02:58:03.300264 IP node2.personal-agent > 192.168.137.1.54964: Flags [S.], seq 1134158746, ack 238464747, win 2560, options [mss 256,nop,nop,sackOK,nop,wscale 7], length 0
02:58:03.300380 IP 192.168.137.1.54964 > node2.personal-agent: Flags [.], ack 1134158747, win 16482, length 0
02:58:04.300892 IP node2.personal-agent > 192.168.137.1.54964: Flags [S.], seq 1134158746, ack 238464747, win 2560, options [mss 256,nop,nop,sackOK,nop,wscale 7], length 0
02:58:04.301380 IP 192.168.137.1.54964 > node2.personal-agent: Flags [.], ack 1134158747, win 16482, options [nop,nop,sack 1 {1134158746:1134158747}], length 0
02:58:06.502862 IP node2.personal-agent > 192.168.137.1.54964: Flags [S.], seq 1134158746, ack 238464747, win 2560, options [mss 256,nop,nop,sackOK,nop,wscale 7], length 0
02:58:06.506196 IP 192.168.137.1.54964 > node2.personal-agent: Flags [.], ack 1134158747, win 16482, options [nop,nop,sack 1 {1134158746:1134158747}], length 0
02:58:10.707021 IP node2.personal-agent > 192.168.137.1.54964: Flags [S.], seq 1134158746, ack 238464747, win 2560, options [mss 256,nop,nop,sackOK,nop,wscale 7], length 0
02:58:10.707480 IP 192.168.137.1.54964 > node2.personal-agent: Flags [.], ack 1134158747, win 16482, options [nop,nop,sack 1 {1134158746:1134158747}], length 0
02:58:18.717827 IP node2.personal-agent > 192.168.137.1.54964: Flags [S.], seq 1134158746, ack 238464747, win 2560, options [mss 256,nop,nop,sackOK,nop,wscale 7], length 0
02:58:18.719636 IP 192.168.137.1.54964 > node2.personal-agent: Flags [.], ack 1134158747, win 16482, options [nop,nop,sack 1 {1134158746:1134158747}], length 0
02:58:34.741120 IP node2.personal-agent > 192.168.137.1.54964: Flags [S.], seq 1134158746, ack 238464747, win 2560, options [mss 256,nop,nop,sackOK,nop,wscale 7], length 0
02:58:34.743136 IP 192.168.137.1.54964 > node2.personal-agent: Flags [.], ack 1134158747, win 16482, options [nop,nop,sack 1 {1134158746:1134158747}], length 0