Ping with java to multiplatform
Asked Answered
P

4

7

I need to perform ping to some ip/machine. The code may be executed on any kind of platform (windows, linux, mac) and i need to get the information about loss and the round trip time. so java's exec of ping is not good enough because then i need to parse ping response as string, which is different per platform and per OS language. so what do i need to do? I know there is something like JNI, but i'm new to java, so need a little help here. it still means i need to have native implementation per platform? Any examples or suggestions?

Phail answered 30/5, 2011 at 13:57 Comment(1)
I have used JNI to get the ping command.Phail
P
0

I have used JNI to get the ping command. I have implemented the ping using native (C) and then wrapped it with JNI. The downgrade is that need to compile the C dll per platform.

Phail answered 22/6, 2011 at 11:25 Comment(0)
E
5

Since java is not platform independent you can ask which OS you have using the System.getProperty("os.name") and parse the response or define the command according to it.

Alternatively you can use isReachable(int timeout) in InetAddress which seems to be the closest implementation to the ICMP ECHO REQUEST, but it will not provide you the information about loss and round trip time.

Another Idea would be to use tracert/tracepath instead of ping to get the round trip? check this thread for some more information.

Epimorphosis answered 30/5, 2011 at 14:38 Comment(1)
isReachable - is best effort, if icmp doesn't work it switches to TCP.Phail
V
1

You can't do real ICMP with pure Java. The reasonably portable jpcap library provides a nice Java interface to native libraries, though, including ICMP (ping).

Vanpelt answered 30/5, 2011 at 14:2 Comment(0)
C
0

I guess parsing is still your easiest option. I would be suprised if you needed more than this regex:

time[<=]([0-9.]+)[ ]?ms

Enclosed in parentheses you will get the time like 1 or 14 or 1.239 which can be fed to Float.parseFloat().

Cark answered 30/5, 2011 at 14:13 Comment(5)
yeah, but if the OS is in Italian? Chinese ? then "time" will not work.Phail
Now that is a problem, still the order of parameters does not change. You have the Windows way and the UNIX way.Cark
Also: do you have to literally PING the server? May be a special UDP echo server is enough. (If you have control over the server).Cark
yeah it must be ping. because the purpose is to messure loss and round trip rather then testing if the server alive.Phail
@Phail The same is true for UDP. Packet loss is also present there (hence the Unreliable). Plus you can send UDP packets even on full throttle which would be the equivalent of flood ping (which can only done by the root user). The things you can't do with UDP: set TTL, set Don't Fragment, set source route. (The last 2 are ipv4-only).Cark
P
0

I have used JNI to get the ping command. I have implemented the ping using native (C) and then wrapped it with JNI. The downgrade is that need to compile the C dll per platform.

Phail answered 22/6, 2011 at 11:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.