Program Getting Stuck At Accept Statement
I've just posted a part of my program. The first time I run the program I could give the input from a client and the server crosses the accept, but when the program runs for second
Solution 1:
I believe what you want in your Memory thread is this:
def__init__ (self):
threading.Thread.__init__ (self)
defrun(self):
global data_queue
mysock.listen(5)
print"waiting for data"whileTrue:
sleep(0.1)
conn, addr = mysock.accept()
print"received connection from client"
self.talk_to_client(conn)
deftalk_to_client(self, conn):
data = conn.recv(1000)
while data != 'quit':
reply = prepare_reply_to_client(data)
data_queue.put(reply)
conn.close() # if we're done with this connection
Notice how I've moved the listen up above the while loop so it only happens once. Your problem is that your second call to listen() conflicts with the first. You should only call listen once. Subsequent accepts will clone the listen socket and use it for a connection. You then close that connection when your done with it, but your listen continues waiting for new connections.
Here's the canonical example in the python docs: https://docs.python.org/2/library/socket.html#example
Updated: Add example of extended interaction with client by writing method talk_to_client(conn)
Post a Comment for "Program Getting Stuck At Accept Statement"