客戶端發送一個PSH,ACK然後 服務器發送一個PSH,ACK和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..<[email protected]@...............CVC.........I|[email protected]
&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..<[email protected]@.<.............C<[email protected]
&3..&3......
12:42:37.684087 IP localhost:33491 > localhost.1234: . ack 1 win 257 <nop,nop,timestamp 640881102 640881101>
[email protected]@...............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..;[email protected]@...............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.(@[email protected]<..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.)@[email protected]<..CVC.....1{.....
&3..&3..
12:42:37.685639 IP localhost:33491 > localhost.1234: . ack 2 win 257 <nop,nop,timestamp 640881104 640881103>
[email protected]@...............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..;[email protected]@...............CVC.C<......./.....
&3%W&3..hello 2
12:42:42.683401 IP localhost.1234 > localhost:33491: R 1128039655:1128039655(0) win 0
E..([email protected]@.<.............C<......P...b...
9 packets captured
27 packets received by filter
0 packets dropped by kernel
在服務器的任一版本中,您是否看到從服務器發送的確認?你提到客戶發送的應答不應該發生,除非你從服務器獲得某些東西。你確定python應用程序的行爲? – stonemetal 2009-09-14 19:10:19