Server process closes the connection as soon as one client is done executing
I am implementing basic socket program , but in this case i want the server process to keep on running . But the problem is as soon 开发者_运维知识库as client is executed , the server also closes the connection .
here`s the server code :
# 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()
The following is the client code
# 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)
I want the server code to keep on running , so that even if i run client code again , the server should process it .
I think you may be able to find your answer from this post on keeping sockets alive.
精彩评论