Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to check if the connection is alive (winsock)

connection

  • Please log in to reply
9 replies to this topic

#1 Kuto

Kuto

    CC Regular

  • Member
  • PipPipPip
  • 48 posts

Posted 23 May 2011 - 10:09 AM

I know only one way to do that so far. Call recv function, if recv returns 0, the connection is closed, if recv returns -1, there is a problem with connection. But unfourtunatley this is not work true.

I have a multithread client program. i call recv function in an infinite loop. i check return value of recv and if its <= 0 , i do whatever i want.

So , if i close server program , recv returns -1, no problem. if i close connected socket in server by calling closesocket func , recv returns 0, still no problem. But if client's internet connection is gone for a while , example if the modem restart itself or wireless connection is broken, that time recv never returns anything.

Also if the server connection is gone, example if the modem restarted and ip changed, client's recv still doesnt return anything, just waits.

To cut a long story short, i want to know another way to check connection is alive anytime, so i will check the connection time to time. Pls another way, not recv. i dont want to set up a new connection with a new socket and port only to check, because it fails same.
Thank u for reading, im waiting for your answers.
  • 0

#2 mebob

mebob

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 467 posts
  • Programming Language:C, C++, Assembly
  • Learning:PHP

Posted 23 May 2011 - 11:35 AM

The only way I can think of is to have some kind of mechanism between the client and the server to see if each other is still online. I don't think that there is something implemented in the TCP protocol to tell if they went offline, besides them not ACKing packets, but I could easily be wrong.
  • 0
Latinamne loqueris?

#3 Kuto

Kuto

    CC Regular

  • Member
  • PipPipPip
  • 48 posts

Posted 23 May 2011 - 01:15 PM

The only way I can think of is to have some kind of mechanism between the client and the server to see if each other is still online. I don't think that there is something implemented in the TCP protocol to tell if they went offline, besides them not ACKing packets, but I could easily be wrong.


Yes i v been thinking of it but windows doesnt allow to use raw sockets, windows xp sp2 and later. so i cannot send syn ack packets etc. is there another way?
  • 0

#4 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 23 May 2011 - 02:23 PM

How about sending packets/messages at regular time intervals, using the already established connection, to note that the connection is still alive. If you don't hear from client in that time you can assume it's dead.
  • 0

The roots of education are bitter, but the fruit is sweet.


#5 Kuto

Kuto

    CC Regular

  • Member
  • PipPipPip
  • 48 posts

Posted 23 May 2011 - 03:09 PM

How about sending packets/messages at regular time intervals, using the already established connection, to note that the connection is still alive. If you don't hear from client in that time you can assume it's dead.


send function writes the network buffer and returns, we cannot understand if connection is broken with this, however recv returns 0 if the connection is broken but only if broken normal ways , for example if someon cut the ethernet cable, recv function never returns, so we cannot know if the connection is alive or not.

i can try to connect at spesific time intervals but i dont want to do that because i use already 2 socket and port , one of them for file transfer and one of them for normal comunication. i will do that if the last chance...
  • 0

#6 mebob

mebob

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 467 posts
  • Programming Language:C, C++, Assembly
  • Learning:PHP

Posted 23 May 2011 - 04:31 PM

If you are using a multithreaded client, then you CAN know. Just have the calling thread monitor the child threads to see how long it takes for a reply.
  • 0
Latinamne loqueris?

#7 Kuto

Kuto

    CC Regular

  • Member
  • PipPipPip
  • 48 posts

Posted 23 May 2011 - 06:17 PM

If you are using a multithreaded client, then you CAN know. Just have the calling thread monitor the child threads to see how long it takes for a reply.


Actually i use select() function for multiclient, i use threads for other jobs. I solved the problem with the bad way , with a new socket and port. server always listens, if any client try to connect, accept it and close connection back. So clients try to connect every 10 second to server, after connection is ok, clients also close connection, i mean they only try to connect, no comunication, if they are able to connect, connection is alive, if not, restart itself..
  • 0

#8 mebob

mebob

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 467 posts
  • Programming Language:C, C++, Assembly
  • Learning:PHP

Posted 24 May 2011 - 05:47 AM

You could use the Windows API asynchronous file functions to write and read from the socket. That way you can time it.
  • 0
Latinamne loqueris?

#9 Kuto

Kuto

    CC Regular

  • Member
  • PipPipPip
  • 48 posts

Posted 24 May 2011 - 08:11 AM

You could use the Windows API asynchronous file functions to write and read from the socket. That way you can time it.


Yes but i need to change my programming style totally to do that, honestly i have no experience about asynchronous communication. Thank for advice, thank also to other replies.
  • 0

#10 ntaryl

ntaryl

    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 19 June 2011 - 10:24 PM

Try to ping the servers .
Send ping and wait for pong
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download