Sign in to follow this  
Jarry

Checking if the connection from host was closed

Recommended Posts

Jarry    122
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.

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Crazyfool    307
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?

Share this post


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

Share this post


Link to post
Share on other sites

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