3
作爲學校任務的一部分,我們需要在python
中實現一個非常簡單的tcp版本。自己進行通信是通過udp
插座完成的。實現tcp的accept()函數
我的問題是實現accept()
,主要支持多個客戶端。我讀了很多,但找不到任何來源來回答我的問題。這裏是我的代碼現在:
# Accepts an incoming connection. This method blocks until an incoming SYN message
# arrives, handles the SYN message and returns the socket.
def accept(self):
if self.state < STCP_STATE_LISTEN:
raise StcpSocketException("Socket not listening")
# Extract syn segment
while True:
syn = stcp_packet.read_from_socket(self.sock)
if syn.syn:
log.debug("(%s,%s) received SYN %s" % (self.local_addr, self.local_port, syn))
break
# TODO allocates the TCP buffers and variables
# Allocate new socket
connection = stcp_socket()
connection.bind(self.local_addr, 0)
connection.remote_addr = syn.srcIp
connection.remote_port = syn.srcPort
connection.change_state(STCP_STATE_SYN_RCVD)
# Send syn ack segment
syn_ack = connection.create_empty_pkt(True, True, False, None, syn.seqNum + 1)
self.seqNum += 1
connection.sock.sendto(syn_ack.pack(), syn.srcIp, syn.srcPort)
log.debug("(%s,%s) sent SYN ACK %s" % (connection.local_addr, connection.local_port, syn_ack))
# Extract last segment in handshake process
while True:
ack = stcp_packet.read_from_socket(connection.sock)
if ack.ack:
log.debug("(%s,%s) received ACK %s" % (connection.local_addr, connection.local_port, ack))
connection.change_state(STCP_STATE_ESTAB)
break
return connection
而且我的問題:
- 應SYN包新的socket迴應,還是應該老(監聽)插口完成三次握手?
- 客戶端是否應該繼續向偵聽套接字發送數據包,而偵聽套接字又將傳遞給新創建的套接字,還是客戶端應該從syn_ack數據包中獲取新套接字的IP和端口並直接發送數據包?
閱讀[accept(2)](http://man7.org/linux/man-pages/man2/accept.2.html)手冊頁。 –