我正在实现基本套接字程序,但在这种情况下,我希望服务器进程继续运行。但问题是,只要客户端执行,服务器也会关闭连接。
这里是服务器代码:
# Echo server program
import socket
HOST = '' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
print data
if not data: break
data1 = " Hello client 1 "
conn.send(data1)
#conn.close()
以下是客户端代码
# Echo client program
import socket
HOST = 'localhost' # The remote host
PORT = 50007 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)
我希望服务器代码继续运行,这样即使我再次运行客户端代码,服务器也应该处理它。