Java Socket getLocalPort() methodThe getLocalPort() method of Java Socket class returns the local port number to which the specified socket is bound. If the socket was being closed, this method will return the connected port number after the socket is closed. SyntaxParameterNA ReturnThe getLocalPort () method either returns the local port number to which the socket is connected, or returns -1 if the socket is not bounded. Example 1Test it NowOutput: Port number: 1085 Example 2Test it NowOutput: Port number: -1 Example 3Test it NowOutput: Exception in thread "main" java.lang.IllegalArgumentException: port out of range:-1085 at java.net.InetSocketAddress.checkPort(InetSocketAddress.java:143) at java.net.InetSocketAddress. Next TopicJava Socket Class |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India