// Create a new socket with the given IP address and port number Socket socket = new Socket("localhost", 8080); // Get the IP address of the local endpoint of this socket InetAddress localAddress = socket.getLocalAddress(); System.out.println("Local IP address: " + localAddress.getHostAddress());
// Create a new server socket with the given port number ServerSocket serverSocket = new ServerSocket(8080); // Get the IP address of the local endpoint of this server socket InetAddress localAddress = serverSocket.getInetAddress(); System.out.println("Local IP address: " + localAddress.getHostAddress());This example creates a new server socket with the port number "8080". The getLocalAddress() method is then called to retrieve the IP address of the local endpoint of the server socket. Finally, the IP address is printed to the console. The java.net.Socket class is part of the java.net package library.