Independent on UDP or TCP the simplest way with Socket is


public int ping(String server, int port) {
try {
Socket s = new Socket(server, port);
...