Sign in to follow this  

Socket disconnections/timeouts

This topic is 4554 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4554 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this