We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › how to ping a host
Page Index Toggle Pages: 1
how to ping a host? (Read 1131 times)
how to ping a host?
Oct 11th, 2007, 12:44am
 
Are there any libraries that offer functions that can tell:
-if a host is alive?
-what is the response time for the connection to the host?

More importantly, can i somehow do this in processing without importing extra libraries?

Any help is much apretiated...
Re: how to ping a host?
Reply #1 - Oct 11th, 2007, 3:36pm
 
a quick google ('java ping') says that there is no way java can be used for pinging a server, at least natively, because the networking protocol is at too high a level. a couple of pages suggest getting around it by calling system functions and parsing the results, 'ping -n 1 hostname' in windows or linux for instance.
Re: how to ping a host?
Reply #2 - Oct 11th, 2007, 4:14pm
 
Whilst you can't ping per-se, you could time how long it takes to get a (simple) webpage from a webserver.

Code:
int start=millis();
loadString("http://www.google.com/");
int end=millis();
println("Took: "+(end-start)+"ms");


That's not so hlepful if the systems you want to time don't have a webserver running, or only serve large pages (but then, you could get a non-existanrt page, since most 404s are small)
Re: how to ping a host?
Reply #3 - Oct 13th, 2007, 5:47pm
 
koogy wrote on Oct 11th, 2007, 3:36pm:
a quick google ('java ping') says that there is no way java can be used for pinging a server, at least natively, because the networking protocol is at too high a level. a couple of pages suggest getting around it by calling system functions and parsing the results, 'ping -n 1 hostname' in windows or linux for instance.


well calling system functions will have to do then, but now i need to find out what is the pinging command for different OS-s and what is the formatting of the results...

Thank you for the help koogs.

@JohnG: Thank you for your advice but i actually need to ping hosts whether or not they had webservers running - i need the response time for the connection to them which is quite different...
Re: how to ping a host?
Reply #4 - Oct 18th, 2007, 4:47am
 
I don't know if this will be helpful for you but I wrote a class to perform a traceroute. It's not the most beautiful code but maybe you will find it useful (works on Windows, Unixes).

The class:
Code:

import java.util.regex.*;

class TraceRoute{
Runtime r = Runtime.getRuntime();
private String url;
private ArrayList hopsList = new ArrayList();
private String hopsString = " ";
private boolean doBucketLimit = false;//if # of consecutive hops == bucketLimit go into the bit bucket abort
private int bucketLimit;

TraceRoute(String myurl){
url = myurl;
}

TraceRoute(){}

public void setURL(String thisurl){
url = thisurl;
}

public void setBucketLimit(int bLimit){
doBucketLimit = true;
bucketLimit = bLimit;
}

public void findRoute(){
String osName = System.getProperty("os.name");
String commandName; //windows ? tracert traceroute

try{
if(osName.equals("Windows XP") || osName.equals("Windows NT") || osName.equals("Windows 2000")){
commandName = "tracert";
}
else{
commandName = "traceroute";
}

Process proc = r.exec(commandName + " " + url);
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
int cantReachCounter = 0;
//reset hopsString + hopsList berfore next traceroute
hopsString = "";
hopsList.clear();
while((line = br.readLine()) != null){
if(doBucketLimit == true){
if(Pattern.matches(".+\\*.\\*.\\*",line)){
cantReachCounter++;
if(cantReachCounter == bucketLimit){
break;
}
}
else{
cantReachCounter = 0;
}
hopsList.add(line);
hopsString += (line + "\r\n");
}
else{
hopsList.add(line);
hopsString += (line + "\r\n");
}
}
}
catch(Throwable t){
t.printStackTrace();
}
}
//method with modifers these are platform specific
public void findRoute(String modifers){
String osName = System.getProperty("os.name");
String commandName;

try{
if(osName.equals("Windows XP") || osName.equals("Windows NT") || osName.equals("Windows 2000")){
commandName = "tracert";
}
else{
commandName = "traceroute";
}
Process proc = r.exec(commandName + " " + modifers + " " + url);
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
int cantReachCounter = 0;
//reset hopsString + hopsList
hopsString = "";
hopsList.clear();
while((line = br.readLine()) != null){
if(doBucketLimit == true){
if(Pattern.matches(".+\\*.\\*.\\*",line)){
cantReachCounter++;
if(cantReachCounter == bucketLimit){
break;
}
}
else{
cantReachCounter = 0;
}
hopsList.add(line);
hopsString += (line + "\r\n");
}
else{
hopsList.add(line);
hopsString += (line + "\r\n");
}
}
}
catch(Throwable t){
t.printStackTrace();
}
}

public ArrayList getRouteList(){
return hopsList;
}
public String getRouteString(){
return hopsString;
}
}


Usage:
Code:

TraceRoute tr;

void setup(){
tr = new TraceRoute();
tr.setBucketLimit(1);
tr.setURL("nytimes.com");
tr.findRoute();
size(200,200);
}

void draw(){
background(0);
String myRoute = tr.getRouteString();
println(myRoute);
noLoop();
}

To use this outside of the processing environment you will need to sign the applet.
Re: how to ping a host?
Reply #5 - Oct 24th, 2007, 6:04pm
 
Thank you, jomo.
Page Index Toggle Pages: 1