My linux world » Java – Network

Java - Network


Open TCP connection on a specific port

try (Socket socket = new Socket(InetAddress.getByName(hostname), port))
{
  // do what you want with socket
}
catch(Exception e) {
  LOGGER.debug("Socket TCP not opened",e);
}

Open UDP connection on a specific port

try (DatagramSocket datagramSocket = new DatagramSocket(port,InetAddress.getByName(hostname))){
  // do what you want with datagramSocket
}
catch(Exception e) {
  LOGGER.debug("Socket UDP not opened",e);
}

Ping

InetAddress.getByName(address).isReachable(timeoutInMilliSeconds);

Copyright © 2024 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.