0

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?

3
  • What is causing the data transmission to pause for a while? Is it because the client has decided to stop sending for a while, or is it due to a dropped-packet/hardware problem (e.g. someone temporarily disconnected an Ethernet cable)? In the latter case, the TCP layer will detect the lost packets and assume there is too much network congestion, and slow down the TCP connection's transmission rate to compensate -- sometimes making it so slow that it appears to be stalled (although it will eventually recover and speed up again, sometime after packets stop getting dropped) Commented Apr 26, 2019 at 14:26
  • The data transmission pause is due to a break for the client sending data. And the socket in my case is in blocking mode (default). Commented Apr 28, 2019 at 2:36
  • You can't keep it alive. You can only avoid closing it. If it fails you have to close it, and a read timeout is an essential defence against both network faiures and rogue clients. Commented Apr 28, 2019 at 3:37

1 Answer 1

2

Try setting timeout along with the better exception handling and reconnection mechanism. You should also refer to setblocking.

Sign up to request clarification or add additional context in comments.

4 Comments

In my case the sockets are in blocking mode by default and I think it's OK to go (few clients). I don't see how the connection would be down and if it did, which timeout triggered that? How can I keep the TCP connection alive?
@Xin I don't know Python, but a read timeout is delivered as an error, not as end of stream, and your present code seems to treat those both as end of stream.
The fact is that the data transmission between client and server is unidirectional and the connection should be considered alive based on the return value of recv(). So how should I add a reconnection mechanism?
@Xin The fact is that the client hasn't sent you any data, or the network hasn't transmitted it to you. From the receiver's point of view these conditions are indistinguishable, and you need to defend against them both, and the only weapon you have in your armoury is a read timeout. Set it to whatever you consider an inordinate response time, typically two or three standard deviations above the mean response time, and disconnect if you get it. If you're the server there is nothing more you can do. If you're the client you can reconnect and try again, subject to the requirement of idempotence.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.