0
我正在實現基本套接字程序,但在這種情況下,我希望服務器進程繼續運行。但問題是客戶端一執行,服務器也關閉連接。服務器進程一旦完成一個客戶端就關閉連接
here`s服務器代碼:
# 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)
我希望服務器代碼繼續運行,這樣,即使我再運行客戶端代碼,服務器應該處理它。
一個很好的解釋套接字的基礎知識和套接字和連接的區別。 – 2011-02-25 06:45:56