Online Java Compiler By
JavaTpoint.com
import java.io.IOException; import java.net.*; public class JavaSocketGetPortExample2 { public static void main(String[] args) throws IOException { Socket socket = new Socket(); //getPort() method will return the port number of this socket //if the socket is not binded, then 0 is returned as a default port number System.out.println("Port number: "+socket.getPort()); } }
Output