Ping a server from client PC?

This is a discussion on "Ping a server from client PC?" within the JavaScript Forum section. This forum, and the thread "Ping a server from client PC? are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Mar 26th, 2007, 17:04
New Member
Join Date: Feb 2007
Location: Wouldn't you like to know Jacko?
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Ping a server from client PC?

I've been trying to write a script using AJAX that will ping a foreign server from the client PC and then return the ping time in ms. I'm having no luck; the only time it works is if I'm connecting to a file on the save server, and that's not much use is it? It will almost always return 0.

Could somebody help me out? The servers I will be pinging are my own, so is there something I can do to let the client's browser know I'm pinging a friendly server? I thought of using subdomain DNS, but haven't tested that yet as my current web host is too nasty to let me do that

Thanks for any help!

Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Javascript & Ajax Ping</title>
<script type="text/javascript">
function ping()
{
  dt = new Date();
  if(window.XMLHttpRequest)
  {
      obj = new XMLHttpRequest();
      obj.open("GET", "http://rapidgameservers.com/serversubdomain", true);       
      obj.send(null);
  } 
  else if(window.ActiveXObject)
  {
      obj = new ActiveXObject("Microsoft.XMLHTTP");
      obj.open("GET", "http://rapidgameservers.com/serversubdomain", true);
      obj.send(null);
  } 
      
    ndt = new Date();
    time = (ndt-dt)/1000+"ms";
    alert(time);
}
</script>
</head>

<body onload="ping()">
</body>
</html>
Reply With Quote

  #2 (permalink)  
Old Mar 27th, 2007, 01:48
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,620
Blog Entries: 1
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: Ping a server from client PC?

The reason you keep getting 0 is because you haven't defined a callback function... I'm not good at explaining but I hope you get this.

The following line sends the request:
obj.send(null);
then the code continues to execute line by line before the response is received. meaning this code...
ndt = new Date();
time = (ndt-dt)/1000+"ms";
alert(time);
... is being processed immediately after the request is sent, not when the response is received, which is what you want.

What you're missing is what is sometimes called a 'callback' function. That is a function which is executed once the request is complete (the response is received)
obj.onreadystatechange = callbackFunctionName;
I haven't tested it, it should look like this:
Code: Select all
function ping()
{
  dt = new Date();

 // callback function to be executed and request is complete

      callback = function()  {
     ndt = new Date();
     time = (ndt-dt)/1000+"ms";
     alert(time);
    }

   if(window.XMLHttpRequest)
  {
      obj = new XMLHttpRequest();
      obj.open("GET", "http://rapidgameservers.com/serversubdomain", true);       
  } 
  else if(window.ActiveXObject)
  {
      obj = new ActiveXObject("Microsoft.XMLHTTP");
      obj.open("GET", "http://rapidgameservers.com/serversubdomain", true);
  } 

     // specify callback function

       obj.onreadystatechange = callback;

      obj.send(null);
}
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)

Last edited by spinal007; Mar 27th, 2007 at 01:52.
Reply With Quote
Reply

Tags
ajax, javascript, ping

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
client mac address or client HDD serial no uday JavaScript Forum 3 Apr 18th, 2008 23:22
URGENT!!!! Send HTTP request from one web server to another server chandra.nowduri ASP.NET Forum 1 Aug 7th, 2006 19:18
Difference between Server & Client Scripting Coty Skaj Other Programming Languages 2 Jul 6th, 2006 17:18
Need to process data on one server, utilize it on another server ... can it be done? jaobrien Classic ASP 1 Dec 6th, 2005 00:58
Client-Side VS Server Side Scripting redev2006 JavaScript Forum 1 Jul 30th, 2005 19:30


All times are GMT. The time now is 19:12.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43