I'm writing a multiprocessing server in python to receive data from multiple clients. When a new connection is established the data transmission is OK at first. A new process is created to receive and deal with the data stream. If the client disconnects the server actively, the socket is closed and process is terminated.
Up until now everything is OK. However, when the data transmission pauses for a while, the recv() function seems down and cannot receive new data any more. However, the connection seems well based on the return value of recv(). Despite that the process still exists, the client have to reconnect the server, which makes the server to create a new process for it.
This is the main code of my server:
def main(): with socket(AF_INET, SOCK_STREAM) as s: s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) address = ('', 7788) s.bind(address) s.listen() while True: print('WAITING FOR CONNECTIONS...') newConn, newAddr = s.accept() with newConn: print('%s CLIENT ACCEPTED...' % newAddr[0]) p = Process(target=recv, args=(newConn, newAddr)) p.start() def recv(newConn, newAddr): while True: recvData = newConn.recv(1024) if len(recvData) > 0: dataprocessing(recvData) else: print('%s CLIENT DISCONNECTED' % newAddr[0]) break I hope every connection can stay alive as long as possible to receive new data from the client at any time. How come my server doesn't work well? It is worth noting that when connection seemed dead, it neither got new data to process nor printed out "CONNECTION DISCONNECTED" information. Why is that?