也许我不了解套接字应该如何工作,所以也许有人可以帮助我。我认为以下内容会永远循环打印“Sent Hello. Received: Hello to you!”,但实际上它循环了两次然后挂起。没有超时,对等方重置连接等。只是挂起。
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!")