import java.net.*; public class Client { public static void main(String[] args) throws Exception { Socket clientSocket = new Socket("localhost", 8080); InetSocketAddress localAddress = (InetSocketAddress) clientSocket.getLocalSocketAddress(); System.out.println("Client local address: " + localAddress.toString()); clientSocket.close(); } }
import java.net.*; public class Server { public static void main(String[] args) throws Exception { ServerSocket serverSocket = new ServerSocket(8080); InetSocketAddress localAddress = (InetSocketAddress) serverSocket.getLocalSocketAddress(); System.out.println("Server local address: " + localAddress.toString()); serverSocket.close(); } }In this example, a server socket is created on port 8080. The getLocalSocketAddress() method is called on the server socket to get its local socket address, which is printed to the console. These examples use the java.net package library for working with internet addresses and sockets.