TIME_WAIT
Mac OS X 上没有
正常情况下,当一个TCP连接关闭时,最先被调用一侧的socketclose()
会保持该TIME_WAIT
状态。
当其中一个对等点是 Mac OS X (Lion) 计算机时TIME_WAIT
,netstat -an
如果close()
if在 Mac 端首先调用。然而,似乎套接字是实际上处于TIME_WAIT
状态,因为尝试再次调用listen()
(不使用套接字选项SO_REUSEADDR
)导致listen()
失败。
等待 2*MSL(最大段生命周期,在 Mac OS X Lion 上为 15 秒,报告sysctl net.inet.tcp.msl
)清除TIME_WAIT
状态,并且listen()
可以再次调用而不会出现错误。
为何我看不到插座TIME_WAIT
?
测试
以下是两个简单的 Python 测试程序。
服务器
#!/usr/bin/env python
import socket
HOST = ''
PORT = 50007
l = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
l.bind((HOST, PORT))
l.listen(1)
print("Listening on %d" % PORT)
(s, _) = l.accept()
print("Connected")
raw_input("Press <enter> to close...")
l.close()
s.close()
print("Closed")
客户
#!/usr/bin/env python
import socket
import sys
HOST = sys.argv[1]
PORT = 50007
print("Opening connection to server")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
raw_input("Press <enter> to close...")
s.close()
print("Closed")
当在两台不同的 Linux 机器上同时运行服务器和客户端时,首先按下<enter>
呼叫的一方会获得预期的结果:close()
TIME_WAIT
$ ./server-timewait.py
Listening on 50007
Connected
Press <enter> to close...
Closed
$ netstat -an | grep 50007
tcp 0 0 172.16.185.219:50007 172.16.185.42:49818 TIME_WAIT
$
当其中一个对等体是 Mac(运行 OS X Lion)时,我从未在 Mac 上看到过在先关闭后TIME_WAIT
运行时的情况。netstat -an | grep 50007
答案1
答案2
这不是一个答案,但有人也许能够从中挖掘出更多信息。
tcpdump -i lo0 -vv port 50007
## Press Enter at the server window
# Server send a FIN (note the flag)
23:33:04.283768 IP (tos 0x0, ttl 64, id 4134, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->2c9c)!)
localhost.50007 > localhost.56030: Flags [F.], cksum 0xfe28 (incorrect -> 0xeff9), seq 1, ack 1, win 9186, options [nop,nop,TS val 432165676 ecr 432157913], length 0
# Client send back ACK
23:33:04.283803 IP (tos 0x0, ttl 64, id 44906, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->8d57)!)
localhost.56030 > localhost.50007: Flags [.], cksum 0xfe28 (incorrect -> 0xd1a6), seq 1, ack 2, win 9186, options [nop,nop,TS val 432165676 ecr 432165676], length 0
# Server confirm the ACK is received
23:33:04.283812 IP (tos 0x0, ttl 64, id 18284, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->f555)!)
localhost.50007 > localhost.56030: Flags [.], cksum 0xfe28 (incorrect -> 0xd1a6), seq 2, ack 1, win 9186, options [nop,nop,TS val 432165676 ecr 432165676], length 0
## After this point, the server process is actually exit but client still running.
## It's strange that re-run server script gives "OSError: [Errno 48] Address already in use"
## and netstat shows this connection is in CLOSE_WAIT status
## Press Enter at the client window
# Client send a FIN to server
23:33:09.731728 IP (tos 0x0, ttl 64, id 51478, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->73ab)!)
localhost.56030 > localhost.50007: Flags [F.], cksum 0xfe28 (incorrect -> 0xbcb6), seq 1, ack 2, win 9186, options [nop,nop,TS val 432171035 ecr 432165676], length 0
# WTH!? Who send back this packet? The server process is closed!
23:33:09.731764 IP (tos 0x0, ttl 64, id 18754, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->f37f)!)
localhost.50007 > localhost.56030: Flags [.], cksum 0xfe28 (incorrect -> 0xa7c7), seq 2, ack 2, win 9186, options [nop,nop,TS val 432171035 ecr 432171035], length 0