Hi,<div><br></div><div>I'm working on creating a server/client bit of software using threading and sockets (it's a project so I can't use something like twisted), and I've run into a slight issue with my server. My server currently looks like this:</div>
<div><br></div><div><div>class ClientThread(threading.Thread):</div><div> def __init__(self, socketdata, server):</div><div> threading.Thread.__init__(self)</div><div> self.conn = socketdata[0]</div><div>
self.details = socketdata[1]</div><div> self.server = server</div><div><br></div><div> def run(self):</div><div> ''' Method called when the Thread.start() method is called. '''</div>
<div> global CONNCOUNT</div><div> CONNCOUNT -= 1</div><div> print("Getting data from {0}:{1}: ".format(self.details[0],</div><div> self.details[1]), end='')</div>
<div> data = self.conn.recv(1024)</div><div> print(data)</div><div> if data == 'quit':</div><div> self.server.stop()</div><div> self.conn.close()</div><div><br></div><div><br>
</div><div>class Server(threading.Thread):</div><div> def __init__(self, port=1500, max_connections=5):</div><div> ''' Setup the server elements. '''</div><div><br></div><div> threading.Thread.__init__(self)</div>
<div> self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</div><div> self.server.bind(('localhost', 1500))</div><div> self.server.listen(5)</div><div> self.keeprunning = True</div>
<div><br></div><div> def run(self):</div><div> global CONNCOUNT</div><div> while self.keeprunning:#CONNCOUNT > 1: </div><div> ClientThread(self.server.accept(), self).start()</div><div> self.stop()</div>
<div><br></div><div> def stop(self):</div><div> ''' Stop the server. '''</div><div><br></div><div> print("Stopping server... maybe...")</div><div> self.keeprunning = False</div>
<div> # Close the socket connection</div><div> self.server.close()</div><div> print("Server stopped.")</div></div><div><br></div><div>(CONNCOUNT is currently not used - I was using it to stop my server)</div>
<div><br></div><div>My issue is that when I send 'quit' the server does (sort of) close, but (I think) the self.server.accept() is blocking, so until I try connecting a few more times, the server doesn't actually finish.</div>
<div><br></div><div>So that's really my only question - how do I make my server completely stop when I'm done? Pointing me towards documentation would be perfectly fine, preferably with a hint about which function(s) I should look at.</div>
<div><br></div><div>TIA</div><div>Wayne</div>