Ping TCP

Started by
6 comments, last by Metus 21 years, 2 months ago
hi again. since my last post i''ve implemented a server/client application with support for 10 + 1 dummy socket connection. my next dilemma is about the connection lifetime. is there any simple way to test a connection from client to server and server to client or do i have to write my own ping-pong function? isn''t TCP smart enough for that?
Ethereal
Advertisement
For what condition do you want to "test" the connection? If the connection goes down (error, or client/server closed it) you will be alerted in such way that read() will return 0 even when the system said it was data to be received.

However, to see that the client/server is awake, you should implement some sort of NOOP command, thet either can send to the other which then respond with an OK.

I'm assuming its a persistant connection(eg: a game, IRC server), so implementing your own ping-pong system would enable other users to see the latency of other users(either as a point for heckling their connection or locating transfer problems).

If you don't know, an IRC server sends out a ping packet like so:

PING [random number]

And to respond the client sends:

PONG [the same number]

You just start timing from when you send the PING and stop when you recieve the PONG, if the timeout goes too high (20seconds), then boot the user.

[edited by - Zanthos on February 8, 2003 5:57:48 AM]
well, as i said i''ve implemented a server with (at the moment) max 10 connections. and if one of those connections dies (timed-out, connection lost) i want to "release" that connection so a new connection can be established.

i was just wondering if there were any special methods to test the connectivity, but as you stated, if recv returns 0 the connection was either lost or no data was sent...
i''ve planned to send data only when a state has changed (like the player moved, got hurt or died)
Ethereal
quote:Original post by Zanthos
I''m assuming its a persistant connection(eg: a game, IRC server), so implementing your own ping-pong system would enable other users to see the latency of other users(either as a point for heckling their connection or locating transfer problems).

If you don''t know, an IRC server sends out a ping packet like so:

PING

And to respond the client sends:

PONG <the same number>

You just start timing from when you send the PING and stop when you recieve the PONG, if the timeout goes too high (20seconds), then boot the user.



yes, it is a simple game server... i''ve got a might-be scheme like this:

1. loop through all the connections
2. ping each connection with the "connected = true" state
3. start an unique timer
4. wait for pong
5. if no pong reply or high pong-time, close the connetion
Ethereal
quote:Original post by Metus
1. loop through all the connections
2. ping each connection with the "connected = true" state
3. start an unique timer
4. wait for pong
5. if no pong reply or high pong-time, close the connetion
Probably a good one. What socket technique do you use? Blocking? Non-blocking? Asyncronous? Threaded?



well, this is the first time at all i code sockets so i''m using the windows standard, but however, i''m using WSAAsyncSelect so i''d guess its a hybrid =)
i''m planning to use threaded UDP as soon as i get this to work... if ever
Ethereal
Ok, then I think what you proposed would suffice. Have a field in each connection object (or whatever you store connection info in) that tells if the client has "ponged in" and clear it. Then, send out the "ping" to all active connections, and set a timer so you get a message to your window proc when the time-out is. When you get the "pong" you somehow mark that client to have done just that. When you get your time-out message, check all connections, and kill off those who didn''t "pong". Then start over.

This topic is closed to new replies.

Advertisement