Handling lost connections(TCP)

Started by
3 comments, last by hplus0603 12 years, 11 months ago
Hey, Im still with that simple class for handling networking..If my connections are all TCP, is the behavior of lost connections well defined? for example, if a connection is lost( the client exploded its computer), will I get an error when attempting to send() from the server( [font="Verdana"][size=2]WSAECONNRESET or [/font][font="Verdana"][size=2]WSAECONNABORTED ) or it can just hang without noticing me?[/font]

[font="Verdana"] Should I create a method only for testing if the sockets are still connected or I can relly on the failures when sending(if send fails, close the socket)?[/font]
Advertisement

Hey, Im still with that simple class for handling networking..If my connections are all TCP, is the behavior of lost connections well defined? for example, if a connection is lost( the client exploded its computer), will I get an error when attempting to send() from the server( [font="Verdana"][color="#000000"]WSAECONNRESET or [/font] [font="Verdana"][color="#000000"]WSAECONNRESET ) or it can just hang without noticing me?[/font]

[font="Verdana"] Should I create a method only for testing if the sockets are still connected or I can relly on the failures when sending(if send fails, close the socket)?[/font]


If the remote end goes away without sending FIN or RST, then the TCP connection will "hang" until it times out. The time-out time on the sending side varies by implementation, and has been known to be measured in hours for certain server systems.
enum Bool { True, False, FileNotFound };

[font="Verdana"]Should I create a method only for testing if the sockets are still connected or I can relly on the failures when sending(if send fails, close the socket)?[/font]

This is generally known as a keep-alive packet. On the server every packet that is received resets the keep alive counter for that client. On the client every time a packet is sent it resets the send keep alive counter. Now if the send keep alive reaches a certain level it'll send a nop (no operation) keep alive packet which would thus reset both the client's send keep alive and the server's keep alive counter. How you implement this is up to you. In a game environment something as simple as an integer per client works well where the decrement and check is done every update on both the client and server.
Since TCP have its all time checks(for resending packets that werent ACK ) why is this not a standard thing, where you could just set the time until it considers a lost connection? Wouldnt this make things easier?

-edit-
Just noticed a keep_alive socket option in msdn..

Since TCP have its all time checks(for resending packets that werent ACK ) why is this not a standard thing, where you could just set the time until it considers a lost connection? Wouldnt this make things easier?

-edit-
Just noticed a keep_alive socket option in msdn..


Yes, there is to SO_KEEPALIVE option. The problem is that parts of the network between your program and the server may not agree. For example, NAT translation tables may time out, or firewall timed rules may expire.

In general, if you're using a game, if a user hasn't been able to send commands for, say, 30 seconds, chances are the user isn't getting up to date with the game, and you might as well disconnect that user, saving server resources for users that are actually active.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement