简体   繁体   中英

in a tcp connection using nodejs net pacakge does socket.setTimeout() get invoked if we don't get an ack?

TLDR : do unacknowledged messages reset the timeout handler or not in nodejs tcp socket? so does socket.setTimeout(60 * 1000) gets reset if we send a message that is not received or does it invoke the timeout listener?

Long Version :

so let's say we created a tcp server and a client in Nodejs, and we want to detect when the one of the sockets has lost internet connction, and since tcp won't auto close, we implemented a ping pong (client sends a ping every minute to server, server replies with pong), now to handle the logic that checks if the connection has indeed died I used a method were on the server we store that last ping time coming from clients, and every 2 min we check if any of them is older that 2 min we declare the connection dead and destroy the socket. and on the client, we set a timeout of 10 seconds (socket.setTimeout(10000)) that is reset when receiving a pong, which invokes the timeout handler, which declares the connection to be disconnected,my question is: let's say has died, so it won't acknowledge the client's ping, would this cause a timeout if the timer for the timeout is longer than the ping intervals?

The socket API (no matter if nodejs, C, ...) has no knowledge if the sent data got actually received or if they got lost. Sending data on a socket only puts these into the write buffer and then the OS kernel cares about delivery. A send is considered successful by the application if the data got successfully written into the write buffer.

This also means that the idle timeout is not affected by success or failure at the TCP level. If some kind of acknowledgement is required at the application level it must be implemented inside the application protocol. If such an acknowledgement is implemented by a the recipient sending some kind of receipt back, then this means socket activity and will thus also cause the idle timeout to be reset.

... we want to detect when the one of the sockets has lost internet connection

As for just detecting a broken connection one might also use TCP keep-alive .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM