try { ServerSocket serverSocket = new ServerSocket(9000); System.out.println(serverSocket.getInetAddress().getHostAddress()); } catch (IOException e) { e.printStackTrace(); }
try { ServerSocket serverSocket = new ServerSocket(9000, 0, InetAddress.getByName("192.168.1.100")); System.out.println(serverSocket.getInetAddress().getHostAddress()); } catch (IOException e) { e.printStackTrace(); }This example creates a ServerSocket on port 9000, but specifies that it should bind to the IP address represented by the supplied InetAddress object (in this case, 192.168.1.100). The InetAddress of this ServerSocket is then retrieved and its host address is printed. Both examples use the java.net package library.