TCP server closes connections

I have this code

class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler): def handle(self): _data = self.request.recv(1024) Utils.log("Received from %s: %s" % (self.client_address, _data)) 

calling him

 kamcon_server = ThreadedTCPServer((HOST, 3011), ThreadedTCPRequestHandler) server_thread = threading.Thread(target = kamcon_server.serve_forever) server_thread.setDaemon(True) server_thread.start() 

I can connect to the host and the server can send data, but when the client sends something to the server, the connection is automatically closed. What for? Thanks.

+4
source share
4 answers

Your handle() method only calls recv() once per connection. If you want to process several messages from the client, you need to execute a loop. You should also think about your protocol so that you can process request / response messages larger than 1024 bytes (e.g. parse _data and find out if you have a complete message, partial buffer requests, etc.).

For instance:

 def handle(self): close = 0 while not close: _data = self.request.recv(1024) if not _data: # EOF, client closed, just return return Utils.log("Received from %s: %s" % (self.client_address, _data)) self.request.send('got %d bytes\r\n' % len(_data)) if 'quit' in _data: close = 1 

Client Session:

 % telnet localhost 3011 hi got 4 bytes bye got 5 bytes telnet> quit 
+10
source

Try this code, it allows multiple connections to the same port and does not close the socket until the client does this:

 import SocketServer import socket, threading class MyTCPHandler(SocketServer.BaseRequestHandler): BUFFER_SIZE = 4096 def handle(self): while 1: #get input with wait if no data data = self.request.recv(self.BUFFER_SIZE) #suspect many more data (try to get all - without stop if no data) if (len(data)==self.BUFFER_SIZE): while 1: try: #error means no more data data += self.request.recv(self.BUFFER_SIZE, socket.MSG_DONTWAIT) except: break #no data found exit loop (posible closed socket) if (data == ""): break #processing input print "%s (%s) wrote: %s" % (self.client_address[0], threading.currentThread().getName(), data.strip()) if __name__ == "__main__": HOST, PORT = "localhost", 9999 server = SocketServer.ThreadingTCPServer((HOST, PORT), MyTCPHandler) server.serve_forever() 

You can also use ForkingTCPServer instead of ThreadingTCPServer.

+2
source

The handle () method is called immediately after a new TCP connection is established, and not once every time there is data. In this method, you must handle all TCP session connections.

Since everything you do reads one piece of data, the getst connection closes when the handle () method returns.

+1
source

See how TCP connections work, at the end of each data stream, the TCP connection closes to tell the listener that there is no more data in this batch. An approximately TCP connection is as follows:

  • Open connection
  • Send data
  • Close connection

If you are just looking at sending data packets, try using UDP instead, so that every packet is read on arrival if you listen to it.

Perhaps you should tell us that you plan to use this server for ...

Anyway, hope this helps.

0
source

All Articles