Sdlnet ping

Started by
4 comments, last by Mr.L 11 years, 11 months ago
how can i get the ping (Latency) of a host in sdlnet?


believe me I've thaught a lot about it, before i've postet it.
Advertisement

how can i get the ping (Latency) of a host in sdlnet?


believe me I've thaught a lot about it, before i've postet it.


have the client send a packet to the server, and record the time at which the packet is sent, have the server send a reply, have the client record the time at which the reply arrives, output the difference.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
but there's a problem:
i've the code is somthing like this
[font=courier new,courier,monospace]void main(){[/font]

[font=courier new,courier,monospace]double ping;[/font]
[font=courier new,courier,monospace]bool sentstate=true;[/font]
[font=courier new,courier,monospace]while(true){[/font]
[font=courier new,courier,monospace] work_0.02_seconds();[/font]
[font=courier new,courier,monospace] double sincesent=getmils();[/font]
[font=courier new,courier,monospace] checksockets(socks);[/font]
[font=courier new,courier,monospace] if (socketready(sock1)&&sentstate){[/font]
[font=courier new,courier,monospace] send("ping");[/font]
[font=courier new,courier,monospace]sendstate=false;[/font]
[font=courier new,courier,monospace] }[/font]
[font=courier new,courier,monospace] if (socketready(sock1)&&!sentstate){[/font]
[font=courier new,courier,monospace]char[123] buffer; [/font]
[font=courier new,courier,monospace]receive(buffer);[/font]
[font=courier new,courier,monospace]if (buffer=="pong"){[/font]
[font=courier new,courier,monospace]ping=getmilis()-sincesent;[/font]
[font=courier new,courier,monospace] [/font]
[font=courier new,courier,monospace]}[/font]
[font=courier new,courier,monospace]sendstate=false;[/font]
[font=courier new,courier,monospace] }[/font]

[font=courier new,courier,monospace] }[/font]
[font=courier new,courier,monospace]}[/font]
then the ping isn't very exakt, so for example it could never be under 0.02 seconds.
but games can be under 20 milisecond, or have i misunderstood something?
Note that the effective ping time includes the latency of the client and the server.

Another option is to send "client timestamp" with the ping message. The server, when receiving it, stores "client timestamp" plus "server timestamp" when it first receives the message. Once it handles the message and sends a reply, it sends "(server new timestamp - server receive timestamp)" and "client original timestamp" in the message. The client, finally, receives the packet, and can timestamp it, and compare to the original timestamp. Subtract the server working time (the difference between server timestamps) from the client timestamp difference, and divide by two; that's a good approximation of the actual latency between client and server processes.

If you for some reason want to avoid including the processing overhead (even though that's something that will always affect actual latency,) you can use high-priority threads on server and client for receiving/timestamping packets.
enum Bool { True, False, FileNotFound };
ok, i try to implement what you said, and then report back, thanks
seems to be working, thanks

This topic is closed to new replies.

Advertisement