How to check if IP is alive in java?

There are 5 devices on my network with different IP addresses. I want to connect to these devices and receive data from them through a TCP / IP socket when they are available on my network. How can I check if they are available in java?

public void setUpConnection() {
    try {
        Socket client = new Socket(hostIp, hostPort);
        socketReader = client.getInputStream();
        socketWriter = new PrintWriter(client.getOutputStream());
    } catch (UnknownHostException e) {
        System.out.println("Error setting up socket connection: unknown host at " +   hostIp);
        System.out.println("host: " + hostIp + "port: " + hostPort);
    } catch (IOException e) {
        System.out.println("Error setting up socket connection: " + e);
        System.out.println("host: " + hostIp + "port:" + hostPort);
    }
}
+5
source share
4 answers
InetAddress.getByName(host).isReachable(timeOut);

Further link here

+15
source

If you just want to check if the host is located, you can use isReachable

+1
source

Java :

  • : , IP-

  • : , , .

isReachable , .

+1

TCP/IP - . 5 . , . , , Socket NIO. , , - , ip ip . , . : , , databae, , : ip , .

Sahoo

0

All Articles