New to Java - How do I write a ping function?

I need a function to ping a location on the hard drive. It needs to return either true or false. Can anyone help me by displaying an example or suggesting where I can learn more about this. I am not sure if this matters but I am programming for OS X.Thanks - Joe
[290 byte] By [grasshopper3] at [2007-9-26 1:20:50]
# 1
err...who would one ping a location on the hard drive?
esmo at 2007-6-29 0:55:36 > top of Java-index,Archived Forums,Java Programming...
# 2
lol I can see that may seem weird. We actually have a special database developed in arev. To communicate between Java and Arev the two programs must ping each other first. This is a very long story :0) Not exactly the best solution I know.
grasshopper3 at 2007-6-29 0:55:36 > top of Java-index,Archived Forums,Java Programming...
# 3
well a true ICMP ping cannot be made in java. Only tcp/udp are supported. So how should this ping look like, because I still don't get it ;)
esmo at 2007-6-29 0:55:36 > top of Java-index,Archived Forums,Java Programming...
# 4

Okay I will try to explain the best I can. I wrote a function in VB that does what I want so I will show you. I am new to Java but I hope this helps.

Function ping(ToLocation as string) as boolean

//Return true if successfull and False if unsuccessfull

Ping = ping(ToLocation)

If not(Ping) then

end

end if

grasshopper3 at 2007-6-29 0:55:36 > top of Java-index,Archived Forums,Java Programming...
# 5

I have also got this code that I am working with, maybe it may help:

try

{

System.out.println("Will attempt to ping " + placeToGo);

Runtime runtime = Runtime.getRuntime();

Process proc = runtime.exec("ping " + placeToGo);

proc.waitFor();

InputStream in = proc.getInputStream();

//Response.Write("You have successfully pinged. ");

System.out.println("You have successfully pinged. ");

return true;

}

catch (Exception e)

{

System.out.println(e.toString());

System.exit(0);

grasshopper3 at 2007-6-29 0:55:36 > top of Java-index,Archived Forums,Java Programming...
# 6

If you know how to do the ping from command line, just place your commands in a command file (example.cmd) and run: cmd.exe is for NT and command.exe is for windows.

Process prcs1 = rt.exec("cmd.exe /c \"example.cmd\"");

try{

prcs1.waitFor();

}

catch(InterruptedException e){

System.out.println("Problem 1");

}

jmennen at 2007-6-29 0:55:36 > top of Java-index,Archived Forums,Java Programming...
# 7

that can work, but I think you'll want to read the output and see if it is successful, something like:

[code]

Process p = Runtime.getRuntime().exec("ping -n 1 -w 5 machine");

BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));

String s;

boolean bSucces = false;

while( (s=r.readLine()) != null) {

if(s.indexOf("(0% ") >= 0) {

bSuccess = true;

}

}

So you are not pinging a harddrive then ;)

But seriously, although this will work on windows, it will not on another platform and the output you get from a command may be different with the language of the windows platform.

esmo at 2007-6-29 0:55:36 > top of Java-index,Archived Forums,Java Programming...
# 8

that can work, but I think you'll want to read the

output and see if it is successful, something like:

Process p = Runtime.getRuntime().exec("ping -n 1 -w 5

machine");

BufferedReader r = new BufferedReader(new

InputStreamReader(p.getInputStream()));

String s;

boolean bSucces = false;

while( (s=r.readLine()) != null) {

if(s.indexOf("(0% ") >= 0) {

bSuccess = true;

}

}

So you are not pinging a harddrive then ;)

But seriously, although this will work on windows, it

will not on another platform and the output you get

from a command may be different with the language of

the windows platform.

Finally, Are you absolutely sure you need to ping? Can't you just try to connect to this database and see if it fails..

esmo at 2007-6-29 0:55:36 > top of Java-index,Archived Forums,Java Programming...
# 9
http://www.geocities.com/SiliconValley/Bit/5716/ping/index_eng.htmlThat's a native ping impl. usable from java. Might be nicer...
esmo at 2007-6-29 0:55:36 > top of Java-index,Archived Forums,Java Programming...
# 10

Grasshopper , if you find any good MAC JAVA forums let me know there are lots of little differences that most

JAVA programmers do not know about.(command line?)

I have put in a request for a board , but I don't know if it'll go unheard.

There is nothing (as far as I can remember) in the

MRJ spec for covering datagrams from sockets.

Ping is also quite unreliable as alot of sys-admins don't allow ping requests, and you would have to do it from an app rather than an applet.

maybe it would be worth connecting and if you can't connect (or you time out) then Sytem.exit(0)

hold up...try java.net.DatagramPacket 's send() and recieve() ... don't know if it works , but it sounds alot better than trying to do MS-DOS from a MAC ey?

as I said before if you find a MAC board , give me a shout.

BTW OSX is built atop of BSD , maybe there is some way to use this , Apple are good at supplying information on OSX (but not 8.6 unfortunatly)

slinqi at 2007-6-29 0:55:36 > top of Java-index,Archived Forums,Java Programming...
# 11

hold up... I re-read you're original post

the harddrive? you can't ping you're own hard drive , not even on UNIX... .java.io.file use exists() returns a boolean.... ..

jus remember that on a MAC the seperator is ' : '

and not ' / ' (UNIX) or ' \ ' (MS-DOS)

there is something in JDirect about native File functions

for MAC , if java.io doesn't cut-it , but java.io is simpler.

slinqi at 2007-6-29 0:55:36 > top of Java-index,Archived Forums,Java Programming...
# 12
hmm. guess I overlooked the OS X part :)
esmo at 2007-6-29 0:55:36 > top of Java-index,Archived Forums,Java Programming...
# 13
if you are wanting to do Database connectivity then you should maybe look @ Enterprise edition JAVA as that is what it does , HOWEVER I don't think there is a EE for the MAC. ask APPLE !
slinqi at 2007-6-29 0:55:36 > top of Java-index,Archived Forums,Java Programming...