I'm trying to run a server application in PC using ServerSocket, for that I trying to get the system's IP address to start the server and to wait for client to connect, for that I've written,
InetAddress inetAddress = InetAddress.getLocalHost();
ServerSocket serverSocket;
if (serverSocket == null)
serverSocket = new ServerSocket(1000, 0, inetAddress);
Socket socket=serverSocket.accept();
Its working correctly in the Window OS, when I try this application in Unix OS its not working for me, I tried to print the IP address using,
System.out.println(inetAddress.getHostAddress);
in Windows OS correct IP address gets printed but in Unix OS, what I got was
127.0.0.1
so the server is not working, I have not tried this in Mac OS, so is there any way to start the server using the system's default IP address in any OS.
Thanks.
This appears to be a pretty common issue. See the following links for some possible solutions:
java InetAddress.getLocalHost(); returns 127.0.0.1 ... how to get REAL IP?
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4665037
Specifically, the work around and evaluation in the sun bug link explains how the function works on linux and how to make sure your linux box is properly set up to return the correct ip when queried from java.
InetAddress localhost = InetAddress.getLocalHost();
// this code assumes IPv4 is used
byte[] ip = localhost.getAddress();
for (int i = 1; i <= 254; i++) {
ip[3] = (byte)i;
InetAddress address = InetAddress.getByAddress(ip);
if (address.isReachable(1000))
{
// machine is turned on and can be pinged
}
else if (!address.getHostAddress().equals(address.getHostName()))
{
// machine is known in a DNS lookup
}
else
{
// the host address and host name are equal, meaning the host name could not be resolved
}
}
Just pass null for the InetAddress. Also be aware that your specified backlog may be adjusted up or down by the system. If you don't have a really good reason for specifying it just use new ServerSocket(0).
Try this example :
import java.net.*;
import java.util.*;
import java.util.regex.Pattern;
public class GetPublicHostname
{
public static void main(String[] args) throws Throwable {
System.out.println("The IP : " + tellMyIP());
}
public static String tellMyIP()
{
NetworkInterface iface = null;
String ethr;
String myip = "";
String regex = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
try
{
for(Enumeration ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();)
{
iface = (NetworkInterface)ifaces.nextElement();
ethr = iface.getDisplayName();
if (Pattern.matches("eth[0-9]", ethr))
{
System.out.println("Interface:" + ethr);
InetAddress ia = null;
for(Enumeration ips = iface.getInetAddresses();ips.hasMoreElements();)
{
ia = (InetAddress)ips.nextElement();
if (Pattern.matches(regex, ia.getCanonicalHostName()))
{
myip = ia.getCanonicalHostName();
return myip;
}
}
}
}
}
catch (SocketException e){}
return myip;
} }
Related
I am using this part of code to ping an ip address in java but only pinging localhost is successful and for the other hosts the program says the host is unreachable.
I disabled my firewall but still having this problem
public static void main(String[] args) throws UnknownHostException, IOException {
String ipAddress = "127.0.0.1";
InetAddress inet = InetAddress.getByName(ipAddress);
System.out.println("Sending Ping Request to " + ipAddress);
System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
ipAddress = "173.194.32.38";
inet = InetAddress.getByName(ipAddress);
System.out.println("Sending Ping Request to " + ipAddress);
System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
}
The output is:
Sending Ping Request to 127.0.0.1
Host is reachable
Sending Ping Request to 173.194.32.38
Host is NOT reachable
InetAddress.isReachable() according to javadoc:
".. A typical implementation will use ICMP ECHO REQUESTs if the
privilege can be obtained, otherwise it will try to establish a TCP
connection on port 7 (Echo) of the destination host..".
Option #1 (ICMP) usually requires administrative (root) rights.
I think this code will help you:
public class PingExample {
public static void main(String[] args){
try{
InetAddress address = InetAddress.getByName("192.168.1.103");
boolean reachable = address.isReachable(10000);
System.out.println("Is host reachable? " + reachable);
} catch (Exception e){
e.printStackTrace();
}
}
}
Check your connectivity. On my Computer this prints REACHABLE for both IP's:
Sending Ping Request to 127.0.0.1
Host is reachable
Sending Ping Request to 173.194.32.38
Host is reachable
EDIT:
You could try modifying the code to use getByAddress() to obtain the address:
public static void main(String[] args) throws UnknownHostException, IOException {
InetAddress inet;
inet = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 });
System.out.println("Sending Ping Request to " + inet);
System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
inet = InetAddress.getByAddress(new byte[] { (byte) 173, (byte) 194, 32, 38 });
System.out.println("Sending Ping Request to " + inet);
System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
}
The getByName() methods may attempt some kind of reverse DNS lookup which may not be possible on your machine, getByAddress() might bypass that.
You can not simply ping in Java as it relies on ICMP, which is sadly not supported in Java
http://mindprod.com/jgloss/ping.html
Use sockets instead
Hope it helps
You can use this method to ping hosts on Windows and other platforms:
private static boolean ping(String host) throws IOException, InterruptedException {
boolean isWindows = System.getProperty("os.name").toLowerCase().contains("win");
ProcessBuilder processBuilder = new ProcessBuilder("ping", isWindows? "-n" : "-c", "1", host);
Process proc = processBuilder.start();
int returnVal = proc.waitFor();
return returnVal == 0;
}
It will work for sure
import java.io.*;
import java.util.*;
public class JavaPingExampleProgram
{
public static void main(String args[])
throws IOException
{
// create the ping command as a list of strings
JavaPingExampleProgram ping = new JavaPingExampleProgram();
List<String> commands = new ArrayList<String>();
commands.add("ping");
commands.add("-c");
commands.add("5");
commands.add("74.125.236.73");
ping.doCommand(commands);
}
public void doCommand(List<String> command)
throws IOException
{
String s = null;
ProcessBuilder pb = new ProcessBuilder(command);
Process process = pb.start();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null)
{
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null)
{
System.out.println(s);
}
}
}
short recommendation:
don't use isReachable(), call the system ping, as proposed in some of the answers above.
long explanation:
ping uses the ICMP network protcol. To use ICMP, a 'raw socket' is needed
standard users are not allowed by the operating system to use raw sockets
the following applies to a fedora 30 linux, windows systems should be similar
if java runs as root, isReachable() actually sends ICMP ping requests
if java does not run as root, isReachable() tries to connect to TCP port 7, known as the echo port. This service is commonly not used any more, trying to use it might yield improper results
any kind of answer to the connection request, also a reject (TCP flag RST) yields a 'true' from isReachable()
some firewalls send RST for any port that is not explicitly open. If this happens, you will get isReachable() == true for a host that does not even exist
further tries to assign the necessary capabilities to a java process:
setcap cap_net_raw+eip java executable (assign the right to use raw sockets)
test: getcap java executable -> 'cap_net_raw+eip' (capability is assigned)
the running java still sends a TCP request to port 7
check of the running java process with getpcaps pid shows that the running java does not have the raw socket capablity. Obviously my setcap has been overridden by some security mechanism
as security requirements are increasing, this is likely to become even more restricted, unless s.b. implements an exception especially for ping (but nothing found on the net so far)
Just an addition to what others have given, even though they work well but in some cases if internet is slow or some unknown network problem exists, some of the codes won't work (isReachable()). But this code mentioned below creates a process which acts as a command line ping (cmd ping) to windows. It works for me in all cases, tried and tested.
Code :-
public class JavaPingApp {
public static void runSystemCommand(String command) {
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader inputStream = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String s = "";
// reading output stream of the command
while ((s = inputStream.readLine()) != null) {
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String ip = "stackoverflow.com"; //Any IP Address on your network / Web
runSystemCommand("ping " + ip);
}
}
Hope it helps, Cheers!!!
Even though this does not rely on ICMP on Windows, this implementation works pretty well with the new Duration API
public static Duration ping(String host) {
Instant startTime = Instant.now();
try {
InetAddress address = InetAddress.getByName(host);
if (address.isReachable(1000)) {
return Duration.between(startTime, Instant.now());
}
} catch (IOException e) {
// Host not available, nothing to do here
}
return Duration.ofDays(1);
}
On linux with oracle-jdk the code the OP submitted uses port 7 when not root and ICMP when root. It does do a real ICMP echo request when run as root as the documentation specifies.
If you running this on a MS machine you may have to run the app as administrator to get the ICMP behaviour.
Here is a method for pinging an IP address in Java that should work on Windows and Unix systems:
import org.apache.commons.lang3.SystemUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class CommandLine
{
/**
* #param ipAddress The internet protocol address to ping
* #return True if the address is responsive, false otherwise
*/
public static boolean isReachable(String ipAddress) throws IOException
{
List<String> command = buildCommand(ipAddress);
ProcessBuilder processBuilder = new ProcessBuilder(command);
Process process = processBuilder.start();
try (BufferedReader standardOutput = new BufferedReader(new InputStreamReader(process.getInputStream())))
{
String outputLine;
while ((outputLine = standardOutput.readLine()) != null)
{
// Picks up Windows and Unix unreachable hosts
if (outputLine.toLowerCase().contains("destination host unreachable"))
{
return false;
}
}
}
return true;
}
private static List<String> buildCommand(String ipAddress)
{
List<String> command = new ArrayList<>();
command.add("ping");
if (SystemUtils.IS_OS_WINDOWS)
{
command.add("-n");
} else if (SystemUtils.IS_OS_UNIX)
{
command.add("-c");
} else
{
throw new UnsupportedOperationException("Unsupported operating system");
}
command.add("1");
command.add(ipAddress);
return command;
}
}
Make sure to add Apache Commons Lang to your dependencies.
I prefer to this way:
/**
*
* #param host
* #return true means ping success,false means ping fail.
* #throws IOException
* #throws InterruptedException
*/
private static boolean ping(String host) throws IOException, InterruptedException {
boolean isWindows = System.getProperty("os.name").toLowerCase().contains("win");
ProcessBuilder processBuilder = new ProcessBuilder("ping", isWindows? "-n" : "-c", "1", host);
Process proc = processBuilder.start();
return proc.waitFor(200, TimeUnit.MILLISECONDS);
}
This way can limit the blocking time to the specific time,such as 200 ms.
It works well in MacOS、Android and Windows, and should used in JDK 1.8.
This idea comes from Mohammad Banisaeid,but I can't comment.
(You must have 50 reputation to comment)
I know this has been answered with previous entries, but for anyone else that comes to this question, I did find a way that did not require having use the "ping" process in windows and then scrubbing the output.
What I did was use JNA to invoke Window's IP helper library to do an ICMP echo
See my own answer to my own similar issue
InetAddress is not always return correct value. It is successful in case of Local Host but for other hosts this shows that the host is unreachable. Try using ping command as given below.
try {
String cmd = "cmd /C ping -n 1 " + ip + " | find \"TTL\"";
Process myProcess = Runtime.getRuntime().exec(cmd);
myProcess.waitFor();
if(myProcess.exitValue() == 0) {
return true;
}
else {
return false;
}
}
catch (Exception e) {
e.printStackTrace();
return false;
}
I tried a couple of options:
Java InetAddress
InetAddress.getByName(ipAddress), the network on windows started misbehaving after trying a couple of times
Java HttpURLConnection
URL siteURL = new URL(url);
connection = (HttpURLConnection) siteURL.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(pingTime);
connection.connect();
code = connection.getResponseCode();
if (code == 200) {
code = 200;
}.
This was reliable but a bit slow
Windows Batch File
I finally settled to creating a batch file on my windows machine with the following contents: ping.exe -n %echoCount% %pingIp%
Then I called the .bat file in my java code using
public int pingBat(Network network) {
ProcessBuilder pb = new ProcessBuilder(pingBatLocation);
Map<String, String> env = pb.environment();
env.put(
"echoCount", noOfPings + "");
env.put(
"pingIp", pingIp);
File outputFile = new File(outputFileLocation);
File errorFile = new File(errorFileLocation);
pb.redirectOutput(outputFile);
pb.redirectError(errorFile);
Process process;
try {
process = pb.start();
process.waitFor();
String finalOutput = printFile(outputFile);
if (finalOutput != null && finalOutput.toLowerCase().contains("reply from")) {
return 200;
} else {
return 202;
}
} catch (IOException e) {
log.debug(e.getMessage());
return 203;
} catch (InterruptedException e) {
log.debug(e.getMessage());
return 204;
}
}
This proved to be the fastest and most reliable way
This should work:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Pinger {
private static String keyWordTolookFor = "average";
public Pinger() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
//Test the ping method on Windows.
System.out.println(ping("192.168.0.1")); }
public String ping(String IP) {
try {
String line;
Process p = Runtime.getRuntime().exec("ping -n 1 " + IP);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while (((line = input.readLine()) != null)) {
if (line.toLowerCase().indexOf(keyWordTolookFor.toLowerCase()) != -1) {
String delims = "[ ]+";
String[] tokens = line.split(delims);
return tokens[tokens.length - 1];
}
}
input.close();
} catch (Exception err) {
err.printStackTrace();
}
return "Offline";
}
}
i am trying to get all ip address of active pcs in a network using java.
try {
InetAddress localhost = InetAddress.getLocalHost();
System.out.println(localhost.toString());
byte[] ip = localhost.getAddress();
for (int i = 1; i <= 254; i++)
{
ip[3] = (byte)i;
InetAddress address = InetAddress.getByAddress(ip);
if (address.isReachable(50))
{
System.out.println(address + " - Pinging... Pinging");
}
else if (!address.getHostAddress().equals(address.getHostName()))
{
System.out.println(address + " - DNS lookup known..");
}
}
}
catch(Exception e)
{
System.out.println(e);
}
This code is taking to much of time, so how to reduce this time
You are using public boolean isReachable(int timeout) throws IOException. Since you have 254 adress to check, in the worst case, this will take 12.7s (254 * 50ms).
You could reduce the timeout value or use more than one thread to do this.
Is it required to use JAVA ? You can create batchscript, which will be simpler. Netstat -a will give you list of all connected machines. You can use Process p = Runtime.getRuntime().exec('<your command>') in Java. Parse the output with ButteredReader class. Also make sure Netstat is installed on your machine.
Here's a possible modification of your code.
8 Pings are launched at the same time. It's possible to do so because you don't need the result of ping(n-1) before launching ping(n).
I think there's still a problem with your ping method, since it takes much more time than 50ms. I don't know enough about the corresponding methods.
import java.net.InetAddress;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.IntStream;
public class ParallelPing
{
public static void main(String[] args) throws InterruptedException, ExecutionException {
IntStream integers = IntStream.rangeClosed(0, 254);
ForkJoinPool forkJoinPool = new ForkJoinPool(8); // How many pings should be launched at the same time?
forkJoinPool.submit(() -> integers
.parallel()
.forEach(i -> ping(i))).get();
}
private static void ping(int i) {
System.out.println("Trying to ping " + i);
try {
InetAddress localhost = InetAddress.getLocalHost();
//System.out.println(localhost.toString());
byte[] ip = localhost.getAddress();
ip[3] = (byte) i;
InetAddress address = InetAddress.getByAddress(ip);
if (address.isReachable(50)) {
System.out.println(address + " - Pinging... Pinging");
} else if (!address.getHostAddress().equals(address.getHostName())) {
System.out.println(address + " - DNS lookup known..");
}
} catch (Exception e) {
System.out.println(e);
}
}
}
I have the following 2 problems in retrieving the ip of a client.
I have create the following code inside a class:
private static InetAddress thisIp;
static{
try {
thisIp = InetAddress.getLocalHost();
System.out.println("MyIp is: " + thisIp);
} catch(UnknownHostException ex) {
}
}
My problems are:
1) The previous code should retrieve the IP address of a client, when I execute it it print the following message:
MyIp is: andrea-virtual-machine/127.0.1.1
Why it begin with andrea-virtual-machine/ ? (I am developing on a virtual machine), is it a problem?
2) In this way I can retrieve only a single IP address but I could have more than a single network card so I could have more than a single IP address but multiple IP addresses
What can I do to handle this situation? I want put all the multiple IP addresses into an ArrayList
Tnx
Andrea
No, it's not a problem, it's simply an output that consists of hostname and IP (hostname/ip). A detail that you might want to read up: The method toString() in the class InetAddress is implemented to return this format.
The following code will list all IP addresses for each of the interfaces in your system (and also stores them in a list that you could then pass on etc...):
public static void main(String[] args) throws InterruptedException, IOException
{
List<String> allIps = new ArrayList<String>();
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements())
{
NetworkInterface n = e.nextElement();
System.out.println(n.getName());
Enumeration<InetAddress> ee = n.getInetAddresses();
while (ee.hasMoreElements())
{
InetAddress i = ee.nextElement();
System.out.println(i.getHostAddress());
allIps.add(i.getHostAddress());
}
}
}
The method boolean isLoopbackAddress() allows you to filter the potentially unwanted loopback addresses.
The returned InetAddress is either a Inet4Address or a Inet6Address, using the instanceof you can figure out if the returned IP is IPv4 or IPv6 format.
if your system is configured with multiple ip then do like this.
try {
InetAddress inet = InetAddress.getLocalHost();
InetAddress[] ips = InetAddress.getAllByName(inet.getCanonicalHostName());
if (ips != null ) {
for (int i = 0; i < ips.length; i++) {
System.out.println(ips[i]);
}
}
} catch (UnknownHostException e) {
}
The hostname listed before the IP, incidentally, is part of INetAddress. You get both the name and the address because you didn't try to show only the address.
I am using this part of code to ping an ip address in java but only pinging localhost is successful and for the other hosts the program says the host is unreachable.
I disabled my firewall but still having this problem
public static void main(String[] args) throws UnknownHostException, IOException {
String ipAddress = "127.0.0.1";
InetAddress inet = InetAddress.getByName(ipAddress);
System.out.println("Sending Ping Request to " + ipAddress);
System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
ipAddress = "173.194.32.38";
inet = InetAddress.getByName(ipAddress);
System.out.println("Sending Ping Request to " + ipAddress);
System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
}
The output is:
Sending Ping Request to 127.0.0.1
Host is reachable
Sending Ping Request to 173.194.32.38
Host is NOT reachable
InetAddress.isReachable() according to javadoc:
".. A typical implementation will use ICMP ECHO REQUESTs if the
privilege can be obtained, otherwise it will try to establish a TCP
connection on port 7 (Echo) of the destination host..".
Option #1 (ICMP) usually requires administrative (root) rights.
I think this code will help you:
public class PingExample {
public static void main(String[] args){
try{
InetAddress address = InetAddress.getByName("192.168.1.103");
boolean reachable = address.isReachable(10000);
System.out.println("Is host reachable? " + reachable);
} catch (Exception e){
e.printStackTrace();
}
}
}
Check your connectivity. On my Computer this prints REACHABLE for both IP's:
Sending Ping Request to 127.0.0.1
Host is reachable
Sending Ping Request to 173.194.32.38
Host is reachable
EDIT:
You could try modifying the code to use getByAddress() to obtain the address:
public static void main(String[] args) throws UnknownHostException, IOException {
InetAddress inet;
inet = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 });
System.out.println("Sending Ping Request to " + inet);
System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
inet = InetAddress.getByAddress(new byte[] { (byte) 173, (byte) 194, 32, 38 });
System.out.println("Sending Ping Request to " + inet);
System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
}
The getByName() methods may attempt some kind of reverse DNS lookup which may not be possible on your machine, getByAddress() might bypass that.
You can not simply ping in Java as it relies on ICMP, which is sadly not supported in Java
http://mindprod.com/jgloss/ping.html
Use sockets instead
Hope it helps
You can use this method to ping hosts on Windows and other platforms:
private static boolean ping(String host) throws IOException, InterruptedException {
boolean isWindows = System.getProperty("os.name").toLowerCase().contains("win");
ProcessBuilder processBuilder = new ProcessBuilder("ping", isWindows? "-n" : "-c", "1", host);
Process proc = processBuilder.start();
int returnVal = proc.waitFor();
return returnVal == 0;
}
It will work for sure
import java.io.*;
import java.util.*;
public class JavaPingExampleProgram
{
public static void main(String args[])
throws IOException
{
// create the ping command as a list of strings
JavaPingExampleProgram ping = new JavaPingExampleProgram();
List<String> commands = new ArrayList<String>();
commands.add("ping");
commands.add("-c");
commands.add("5");
commands.add("74.125.236.73");
ping.doCommand(commands);
}
public void doCommand(List<String> command)
throws IOException
{
String s = null;
ProcessBuilder pb = new ProcessBuilder(command);
Process process = pb.start();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null)
{
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null)
{
System.out.println(s);
}
}
}
short recommendation:
don't use isReachable(), call the system ping, as proposed in some of the answers above.
long explanation:
ping uses the ICMP network protcol. To use ICMP, a 'raw socket' is needed
standard users are not allowed by the operating system to use raw sockets
the following applies to a fedora 30 linux, windows systems should be similar
if java runs as root, isReachable() actually sends ICMP ping requests
if java does not run as root, isReachable() tries to connect to TCP port 7, known as the echo port. This service is commonly not used any more, trying to use it might yield improper results
any kind of answer to the connection request, also a reject (TCP flag RST) yields a 'true' from isReachable()
some firewalls send RST for any port that is not explicitly open. If this happens, you will get isReachable() == true for a host that does not even exist
further tries to assign the necessary capabilities to a java process:
setcap cap_net_raw+eip java executable (assign the right to use raw sockets)
test: getcap java executable -> 'cap_net_raw+eip' (capability is assigned)
the running java still sends a TCP request to port 7
check of the running java process with getpcaps pid shows that the running java does not have the raw socket capablity. Obviously my setcap has been overridden by some security mechanism
as security requirements are increasing, this is likely to become even more restricted, unless s.b. implements an exception especially for ping (but nothing found on the net so far)
Just an addition to what others have given, even though they work well but in some cases if internet is slow or some unknown network problem exists, some of the codes won't work (isReachable()). But this code mentioned below creates a process which acts as a command line ping (cmd ping) to windows. It works for me in all cases, tried and tested.
Code :-
public class JavaPingApp {
public static void runSystemCommand(String command) {
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader inputStream = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String s = "";
// reading output stream of the command
while ((s = inputStream.readLine()) != null) {
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String ip = "stackoverflow.com"; //Any IP Address on your network / Web
runSystemCommand("ping " + ip);
}
}
Hope it helps, Cheers!!!
Even though this does not rely on ICMP on Windows, this implementation works pretty well with the new Duration API
public static Duration ping(String host) {
Instant startTime = Instant.now();
try {
InetAddress address = InetAddress.getByName(host);
if (address.isReachable(1000)) {
return Duration.between(startTime, Instant.now());
}
} catch (IOException e) {
// Host not available, nothing to do here
}
return Duration.ofDays(1);
}
On linux with oracle-jdk the code the OP submitted uses port 7 when not root and ICMP when root. It does do a real ICMP echo request when run as root as the documentation specifies.
If you running this on a MS machine you may have to run the app as administrator to get the ICMP behaviour.
Here is a method for pinging an IP address in Java that should work on Windows and Unix systems:
import org.apache.commons.lang3.SystemUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class CommandLine
{
/**
* #param ipAddress The internet protocol address to ping
* #return True if the address is responsive, false otherwise
*/
public static boolean isReachable(String ipAddress) throws IOException
{
List<String> command = buildCommand(ipAddress);
ProcessBuilder processBuilder = new ProcessBuilder(command);
Process process = processBuilder.start();
try (BufferedReader standardOutput = new BufferedReader(new InputStreamReader(process.getInputStream())))
{
String outputLine;
while ((outputLine = standardOutput.readLine()) != null)
{
// Picks up Windows and Unix unreachable hosts
if (outputLine.toLowerCase().contains("destination host unreachable"))
{
return false;
}
}
}
return true;
}
private static List<String> buildCommand(String ipAddress)
{
List<String> command = new ArrayList<>();
command.add("ping");
if (SystemUtils.IS_OS_WINDOWS)
{
command.add("-n");
} else if (SystemUtils.IS_OS_UNIX)
{
command.add("-c");
} else
{
throw new UnsupportedOperationException("Unsupported operating system");
}
command.add("1");
command.add(ipAddress);
return command;
}
}
Make sure to add Apache Commons Lang to your dependencies.
I prefer to this way:
/**
*
* #param host
* #return true means ping success,false means ping fail.
* #throws IOException
* #throws InterruptedException
*/
private static boolean ping(String host) throws IOException, InterruptedException {
boolean isWindows = System.getProperty("os.name").toLowerCase().contains("win");
ProcessBuilder processBuilder = new ProcessBuilder("ping", isWindows? "-n" : "-c", "1", host);
Process proc = processBuilder.start();
return proc.waitFor(200, TimeUnit.MILLISECONDS);
}
This way can limit the blocking time to the specific time,such as 200 ms.
It works well in MacOS、Android and Windows, and should used in JDK 1.8.
This idea comes from Mohammad Banisaeid,but I can't comment.
(You must have 50 reputation to comment)
I know this has been answered with previous entries, but for anyone else that comes to this question, I did find a way that did not require having use the "ping" process in windows and then scrubbing the output.
What I did was use JNA to invoke Window's IP helper library to do an ICMP echo
See my own answer to my own similar issue
InetAddress is not always return correct value. It is successful in case of Local Host but for other hosts this shows that the host is unreachable. Try using ping command as given below.
try {
String cmd = "cmd /C ping -n 1 " + ip + " | find \"TTL\"";
Process myProcess = Runtime.getRuntime().exec(cmd);
myProcess.waitFor();
if(myProcess.exitValue() == 0) {
return true;
}
else {
return false;
}
}
catch (Exception e) {
e.printStackTrace();
return false;
}
I tried a couple of options:
Java InetAddress
InetAddress.getByName(ipAddress), the network on windows started misbehaving after trying a couple of times
Java HttpURLConnection
URL siteURL = new URL(url);
connection = (HttpURLConnection) siteURL.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(pingTime);
connection.connect();
code = connection.getResponseCode();
if (code == 200) {
code = 200;
}.
This was reliable but a bit slow
Windows Batch File
I finally settled to creating a batch file on my windows machine with the following contents: ping.exe -n %echoCount% %pingIp%
Then I called the .bat file in my java code using
public int pingBat(Network network) {
ProcessBuilder pb = new ProcessBuilder(pingBatLocation);
Map<String, String> env = pb.environment();
env.put(
"echoCount", noOfPings + "");
env.put(
"pingIp", pingIp);
File outputFile = new File(outputFileLocation);
File errorFile = new File(errorFileLocation);
pb.redirectOutput(outputFile);
pb.redirectError(errorFile);
Process process;
try {
process = pb.start();
process.waitFor();
String finalOutput = printFile(outputFile);
if (finalOutput != null && finalOutput.toLowerCase().contains("reply from")) {
return 200;
} else {
return 202;
}
} catch (IOException e) {
log.debug(e.getMessage());
return 203;
} catch (InterruptedException e) {
log.debug(e.getMessage());
return 204;
}
}
This proved to be the fastest and most reliable way
This should work:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Pinger {
private static String keyWordTolookFor = "average";
public Pinger() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
//Test the ping method on Windows.
System.out.println(ping("192.168.0.1")); }
public String ping(String IP) {
try {
String line;
Process p = Runtime.getRuntime().exec("ping -n 1 " + IP);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while (((line = input.readLine()) != null)) {
if (line.toLowerCase().indexOf(keyWordTolookFor.toLowerCase()) != -1) {
String delims = "[ ]+";
String[] tokens = line.split(delims);
return tokens[tokens.length - 1];
}
}
input.close();
} catch (Exception err) {
err.printStackTrace();
}
return "Offline";
}
}
How can i get the LAN IP-address of a computer using Java? I want the IP-address which is connected to the router and the rest of the network.
I've tried something like this:
Socket s = new Socket("www.google.com", 80);
String ip = s.getLocalAddress().getHostAddress();
s.close();
This seem to work on some cases, but sometimes it returns the loopback-address or something completely different. Also, it requires internet connection.
Does anyone got a more accurate method of doing this?
EDIT: Thought it would be better to ask here than in a comment..
What if you got many interfaces? For example, one for cable, one for wifi and one for virtual box or so. Is it impossible to actually see which one is connected to the network?
Try java.net.NetworkInterface
import java.net.NetworkInterface;
...
for (
final Enumeration< NetworkInterface > interfaces =
NetworkInterface.getNetworkInterfaces( );
interfaces.hasMoreElements( );
)
{
final NetworkInterface cur = interfaces.nextElement( );
if ( cur.isLoopback( ) )
{
continue;
}
System.out.println( "interface " + cur.getName( ) );
for ( final InterfaceAddress addr : cur.getInterfaceAddresses( ) )
{
final InetAddress inet_addr = addr.getAddress( );
if ( !( inet_addr instanceof Inet4Address ) )
{
continue;
}
System.out.println(
" address: " + inet_addr.getHostAddress( ) +
"/" + addr.getNetworkPrefixLength( )
);
System.out.println(
" broadcast address: " +
addr.getBroadcast( ).getHostAddress( )
);
}
}
At first: There is no single address. Your machine has at least two adresses (127.0.0.1 on "lo" and maybe 192.168.1.1 on "eth1").
You want this: Listing network interfaces
As you may expect, you cannot automatically detect which one is connected to any of your routers, since this needs potentially complex parsing of your routing tables. But if you just want any non-local address this should be enought. To be sure, try to use this at least one time on vista or Windows 7, since they add IPv6 addresses.
import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;
public class ListNets
{
public static void main(String args[]) throws SocketException {
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets))
displayInterfaceInformation(netint);
}
static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
out.printf("Display name: %s\n", netint.getDisplayName());
out.printf("Name: %s\n", netint.getName());
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
for (InetAddress inetAddress : Collections.list(inetAddresses)) {
out.printf("InetAddress: %s\n", inetAddress);
}
out.printf("\n");
}
}
The following is sample output from the example program:
Display name: bge0
Name: bge0
InetAddress: /fe80:0:0:0:203:baff:fef2:e99d%2
InetAddress: /121.153.225.59
Display name: lo0
Name: lo0
InetAddress: /0:0:0:0:0:0:0:1%1
InetAddress: /127.0.0.1
This is a method I've used for a while. It includes a little hack to figure out the externally visible ip-address as well.
private List<String> getLocalHostAddresses() {
List<String> addresses = new ArrayList<String>();
try {
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
NetworkInterface ni = e.nextElement();
Enumeration<InetAddress> e2 = ni.getInetAddresses();
while (e2.hasMoreElements())
addresses.add(e2.nextElement().getHostAddress());
}
URL u = new URL("http://whatismyip.org");
BufferedReader in = new BufferedReader(new InputStreamReader(
u.openStream()));
addresses.add(in.readLine());
in.close();
} catch (Exception ignore) {
}
return addresses;
}
try {
InetAddress addr = InetAddress.getLocalHost();
// Get IP Address
byte[] ipAddr = addr.getAddress();
// Get hostname
String hostname = addr.getHostName();
} catch (UnknownHostException e) {
}
As Daniel already pointed out, you cannot know which interface is the one "connected". What if, for example, the computer has multiple network interface cards which are both connected to separate physical LANs?
Let the user decide which interface to use or try them all, depending on what your use case is.