1
也許我不明白套接字應該如何工作,所以也許有人可以幫助我。我雖然認爲以下內容會循環印刷:「發送Hello!收到:Hello to you!」,但實際上它會循環兩次然後掛起。沒有超時,連接重置等。只是掛起。Python套接字問題
import socket
socket_list = []
for i in range(60000, 60002):
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
soc.connect("",i)
socket_list.append(soc)
while True:
for soc in socket_list:
soc.sendall("Hello.")
msg = soc.recv(1024)
print "Sent Hello. Received:",msg
在連接的另一端的代碼很簡單:
import socket
soc = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
soc.bind("localhost",6000x)
while True:
soc.listen(1)
conn, addr = soc.accept()
msg = conn.recv(1024)
if msg == "Hello":
conn.sendall("Hello to you!")
非常感謝。這確實是問題所在。 – Chris