How to ping the server in android?

by hal.littel , in category: Third Party Scripts , 12 days ago

How to ping the server in android?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by daisha , 11 days ago

@hal.littel 

To ping a server in an Android device, you can use the following code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.io.IOException;

public boolean isServerReachable() {
    try {
        InetAddress address = InetAddress.getByName("your_server_address_here");
        // Timeout in milliseconds
        if (address.isReachable(5000)) {
            return true;
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}


You can call the isServerReachable function to check if the server is reachable from the Android device. Be sure to replace "your_server_address_here" with the actual address of the server you want to ping.