Checking if the connection from host was closed

Started by
7 comments, last by Jarry 17 years, 1 month ago
Hello, I would like to know how to check if the connection from host (with winsock in Windows) is closed by the host. I have permanent working connection on IRC, but sometimes it could disconnect, I would like to check when this disconnection happened (to close the socket from my client application and act on purpose). Thanks for help.
Advertisement
You can tell on send() and recv().

Let me find my actual code to find out the specifics.


I haven't done any client work for a while, but this is my basic understand for send:

it returns number of bytes sent, if it returns 0 or a negative number, either the player lsot connection or the server was shut down.

For recv, if it returns 0 or a negative number, either the player lost connection or the server was shut down. Recv also returns number of bytes received.
Is there no other way to know ? I mean, noticing when port is being closed by server or anything else ?

Because I need to to know quite instantly when connection is lost to reconnect, if I while a send(), might take memory and connection bandwith, so slow down the application :/

Thank you.
There might be, but not that i know of. In the past i have implemented a ping pong system where i send a small packet to the client and wait for a short period of time for a delay. If it fails say 3 times then the client can be assumed dead.
I have more than one bot making a connection with the same server. Do you adivce me to do a thread making a ping / pong like every 5 mins for each connection (bot), tough I don't think IRC servers allow pings.
If you used worker threads you would only need one to do all the pinging and yes IRC does sort pinging. Though you could just do it to the IP address of the machines without worrying about using the IRC protocol. Thats assuming you are writing hte IRC client yourself.
I haven't used threads but fd_set and select(), but I guess I won't be able to find out which one has gone down with this system without running an extern function to ping.

My IRC client has been self written. But how can you do it with the IP address of the machine please ? Cause only one client could be down. Would you please mind detailling ?

Thanks again.
Arent you at least calling some sort of recv every frame?

Depending on your program lay out, you should be calling recv in a while loop, and it should probably be non-nblocking. So you can easily know right away.


You're usinmg Select() on a client? I thought Select() was only useful for a server?
I am using Select(), then FD_ISSET() to check for which connection is active. It tells which bot (connection) recieved data, then I use recv(). I'm using blocking sockets unfortunately.

This topic is closed to new replies.

Advertisement