Online Java Compiler By
JavaTpoint.com
import java.io.IOException; import java.net.*; public class JavaSocketGetLocalPortExample2 { public static void main(String[] args) throws IOException { Socket socket = new Socket(); //getLocalPort() method returns the port number of the socket //returns -1 if the socket is not bounded System.out.println("Port number: "+socket.getLocalPort()); } }
Output