By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,918 Members | 1,852 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,918 IT Pros & Developers. It's quick & easy.

A ping program in javascript?

P: n/a
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.

Jul 23 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Egil Hansen wrote:
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.

Jul 23 '05 #2

P: n/a
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?

Jul 23 '05 #3

P: n/a


Egil Hansen wrote:
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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #4

P: n/a
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.

Jul 23 '05 #5

P: n/a
Egil Hansen wrote:
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.

Jul 23 '05 #6

P: n/a
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.

Jul 23 '05 #7

P: n/a
On Wed, 20 Jul 2005 04:41:16 -0700, Egil Hansen wrote:
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.

--
"Blessed is he who expects nothing, for he shall never be disappointed."
Benjamin Franklin (I didn't know he was a Buddhist)

Jul 23 '05 #8

P: n/a


Egil Hansen wrote:
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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #9

P: n/a
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
Egil Hansen wrote:
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.


Jul 23 '05 #10

P: n/a
fj

"Egil Hansen" <eg**@dailyrush.dk> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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.
Jul 23 '05 #11

P: n/a
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

Jul 23 '05 #12

P: n/a
fj said the following on 7/20/2005 2:15 PM:
"Egil Hansen" <eg**@dailyrush.dk> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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.


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.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #13

P: n/a
VK
Egil Hansen wrote:
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.

Jul 23 '05 #14

P: n/a
Hi all

Just wanted to thank everyone for there input. Thanks, Egil.

Jul 25 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.