Socket disconnections/timeouts

Started by
1 comment, last by Raduprv 18 years, 10 months ago
Greetings all! I need help with three things regarding socket programming. 1. How can I tell when a connection has been closed from the other side? 2. How should I handle connections that timeout. E.g a client has a power failure and does not disconnect properly. 3. Should the server periodically ask the client if it's 'there'? i.e sending querying data every second or so? I was thinking of this as an answer to question #2 and counting time elapsed since last reponse. If so, how often? If possible could you tell me how to do this without relying on win32/winsock functions? Thanks
Advertisement
Quote:
1. How can I tell when a connection has been closed from the other side?


You can't, unless it's a TCP connection, then you'll get a receive from that socket where the received bytes is zero.

int bytes = socket.Receive(...);if(bytes == 0){	// Socket is dead	// Free resources on close from your side	socket.Close()}


If it's UDP you'll keep a heartbeat on the connection, every x units of time, you send a SYN (synchronize) packet to your client, and if they don't reply with an ACK (acknowledgement) before a certain amount of time passes, you disconnect them.

Quote:
2. How should I handle connections that timeout. E.g a client has a power failure and does not disconnect properly.


Use the SYN-ACK system above.

Quote:
3. Should the server periodically ask the client if it's 'there'? i.e sending querying data every second or so? I was thinking of this as an answer to question #2 and counting time elapsed since last reponse. If so, how often?


How often is completely up to you. How important is it to boot idle sockets? How often does your program usually require responses? If it's an FPS you could make the timeout rather as they need to be fairly well synchronized to play. A SYN packet still takes time and bandwidth though, so you don't want to be sending them as fast you can.

I made a very simple UDP structure where input and output buffers were flushed once every game loop. If there was no input in the input buffer when the network states were being updated then I sent a SYN packet and logged the time. Every update I would continue to send SYN packets if there was no input (incase the first ones got lost) until the the time passed since the first SYN packet was greater than my timeout, which I think was 10 seconds.

Dropping connections for me wasn't all that important though. 10 second delay in a FPS would be death for them anyway.
-------------------------------------------------------------------Life is short so go on and live it, cause the chicks dig it.- Kahsm
Quote:Original post by Kahsm
Dropping connections for me wasn't all that important though. 10 second delay in a FPS would be death for them anyway.


Yes, but there is no point in keeping unconnected clients to the server. It's a waste of resources.

This topic is closed to new replies.

Advertisement