A ping program in javascript?

E

Egil Hansen

Hi

I'm wondering if its possible to write a javascript, that can ping a ip
and return the average round trip time (like ping in dos/linux)?

Regards, Egil.
 
C

Christopher J. Hahn

Egil said:
Hi

I'm wondering if its possible to write a javascript, that can ping a ip
and return the average round trip time (like ping in dos/linux)?

Regards, Egil.


Sorry, no... JS isn't capable of anything like this unless the host
(usually a browser, but not necessarily) provides some interface to do
it.
 
E

Egil Hansen

So, if I want to allow my website visitors to ping a server and get the
round trip time, i have to use a java applet?
 
M

Martin Honnen

Egil said:
So, if I want to allow my website visitors to ping a server and get the
round trip time, i have to use a java applet?

Not sure whether Java allows ping requests by now but an applet is with
normal security settings only allowed to connect back to the server the
HTML document with the applet has been loaded from.
If you want to allow your visitors to use ping the do the ping on the
server and simply provide an interface to your server-side program.
 
E

Egil Hansen

Hi Martin

Thanks for the reply. I wasn't aware of the security issues.

It would be easy to make it server side, but that's not really what I
want. I want to allow the users to ping a server (gaming server), so
they can know, how good or bad their connection to it is. Do you see
any way doing this, so it won't be too much of a hazel for the users
(e.g. changing security settings/giving my site special permissions)?

PS. Thanks to Christopher as well for his initial answer.
 
C

Christopher J. Hahn

Egil said:
Hi Martin

Thanks for the reply. I wasn't aware of the security issues.

It would be easy to make it server side, but that's not really what I
want. I want to allow the users to ping a server (gaming server), so
they can know, how good or bad their connection to it is. Do you see
any way doing this, so it won't be too much of a hazel for the users
(e.g. changing security settings/giving my site special permissions)?

PS. Thanks to Christopher as well for his initial answer.

Given what Martin Honnen said (I assume he's right because I know
nothing of Java and he's a fairly smart guy), if your game server is
also the web server, it sounds like that would satisfy the security
concerns and the applet would be allowed to do the ping or something
like it.

Now, knowing that your end goal is getting some feel for communications
performance between the end user and the server...

If you can't do it in Java, you might consider something *remotely*
similar, like measuring how long it takes to download a file of known
size from the server:
The game server should serve, by FTP or (preferably) HTTP, say... an
image of at least a couple hundred kilobytes. Then, on the host web
page, you could do something like this:

var I = new Image();
I.onload = function () {
T[ 1 ] = Number( new Date() );
// now do something with T[ 1 ] - T[ 0 ]
}

var T = [ Number( new Date() ) ];

I.src = 'http://gamehost.domain.tld/test_image.img?' +
Math.round( Math.random() * 10000 );


The result is a two-element array T containing 0: the time just prior
to telling it to start downloading the image and 1: the time just after
it finished.

So T[ 1 ] - T[ 0 ] = the number of milliseconds between those two
times.

It's not 100% accurate, but then... neither is ping, really. What you
want is for the user to get a *feel* for how it'll go, right?

Of course, you'll need to give them something to measure that against,
so you'll want to be able to say, "this is optimal," "this is
super-fast," "if it's this slow, don't even bother," etc.


I'm sure some other guys here can brush up the code a bit and probably
make it a bit more accurate or cross-compatible, but it's an idea.

Good luck to you.
 
E

Egil Hansen

Hi again Christopher

Interesting approach, but it won't work in my case, as I have no
control over the gaming servers I would like to allow my users to ping.
It could be totally random servers (users will be able to add servers
to the website them self).

Thanks though, regard, Egil.
 
I

Ivan Marsh

Hi Martin

Thanks for the reply. I wasn't aware of the security issues.

It would be easy to make it server side, but that's not really what I
want. I want to allow the users to ping a server (gaming server), so
they can know, how good or bad their connection to it is. Do you see
any way doing this, so it won't be too much of a hazel for the users
(e.g. changing security settings/giving my site special permissions)?

The server would have to ping the client... assuming the client allows
icmp.
 
M

Martin Honnen

Egil said:
I want to allow the users to ping a server (gaming server), so
they can know, how good or bad their connection to it is. Do you see
any way doing this, so it won't be too much of a hazel for the users
(e.g. changing security settings/giving my site special permissions)?

No, inside the browser whether you use Java or JavaScript with normal
security settings you can only connect back to the server your document
has been loaded from.
Java 1.5 seems to have some ICMP support now with the method isReachable
of the InetAddress class but unless you sign your applet I don't think
there is any chance to use that method in the browser to check abritrary
hosts.
 
C

Csaba Gabor

How about something like this for javascript ping times:

<body onload="alert('body loaded');
window.startTime=new Date();
var imgSrc='http://us.i1.yimg.com/us.yimg.com'+
'/i/us/nt/hdr/stw.gif'+
'?cacheBuster='+Math.random();
document.getElementById('myPingTest').src=imgSrc">
<IMG id=myPingTest
onload="var endTime=new Date();
alert('ping time in milliseconds: '+
(endTime.getMilliseconds()-
window.startTime.getMilliseconds()))">
</body>


It assumes a cooperating server, of course, plus there is additional
delay due to server processing. Still... it may give you a rough
idea. As implied by the "cacheBuster" (which line is there solely to
prevent browser cacheing), I would be very wary of cacheing effects
both of the image and the path taken to the server.

Csaba Gabor from Vienna
 
F

fj

Egil Hansen said:
Hi

I'm wondering if its possible to write a javascript, that can ping a ip
and return the average round trip time (like ping in dos/linux)?

Regards, Egil.

Yes you can.

using AJAX.
 
C

Csaba Gabor

Here is a revised way of pinging yahoo using javascript and iframes
that seems to work with Firefox (but Not IE 6) 1.0+ (Deer Park Alpha 1
rv.1.8b2 dated 20 Jun 2005). Pretty nifty that FF has an onload in the
<IFRAME ...> element.
Regarding my prior (post) example... Evidently .getMilliseconds is not
the way to go, despite Danny Goodman saying that it is equivalent to
..getTime


<html><head><title>Ping Testing</title>
<script type='text/javascript'>
function pingTime() {
var endTime=new Date();
alert('ping time in milliseconds: '+
(endTime.getTime()-window.startTime)
+"\nendTime: "+endTime.getTime()
+"\nstartTime: "+window.startTime)
}
</script>
</head>
<body onload="alert('body loaded');
var startTime=new Date();
window.startTime=startTime.getTime()
var imgSrc='http://yahoo.com'+
'?cacheBuster='+Math.random();
var iFrame=document.getElementById('myPingTest');
iFrame.onload=pingTime;
iFrame.src=imgSrc">
<IFRAME id=myPingTest>
</IFRAME>
</body>
</html>

Csaba Gabor from Vienna
 
R

Randy Webb

fj said the following on 7/20/2005 2:15 PM:
Yes you can.

Prove it.
using AJAX.

"AJAX" is less reliable than other ways of attempting to detect download
speed, all of which has variables to it that you do not (and can not)
control thereby making it impossible to determine reliably.
 
V

VK

Egil said:
Hi again Christopher

Interesting approach, but it won't work in my case, as I have no
control over the gaming servers I would like to allow my users to ping.
It could be totally random servers (users will be able to add servers
to the website them self).

Thanks though, regard, Egil.

Besides other things, "timed download" pseudo-ping doesn't tell too
much about future game quality. It just show the approximate
responsability of the server on port 80 (HTTP)

It can be rather good on 80 (HTTP), dead slow on 21 (FTP) and totally
dead on say 12345 where the game server is running.

The only help would be a signed applet to emulate socket connection to
the game port.

Staying within JavaScript, you could implement a self-updating ranging
system on your servers' list. So each registered user could make his
finger up/finger down after the game. That would also help to
destribute the load more or less evenly between reliable servers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top