DatagramSocket GetLocalPort() Example
Returns the port number on the local host to which this socket is bound.
DatagramSocketclass GetLocalPort() method example. This example shows you how to use GetLocalPort() method.This method Returns the port number on the local host to which this socket is bound.
Here is the code:-
/**
* @Program that Returns the port number on the local host to which this socket
* is bound
* GetLocalPort.java
* Author:-RoseIndia Team
* Date:-20-jun-2008
*/
import java.net.*;
public class GetLocalPort {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket();
int i = ds.getLocalPort();
System.out.println("Port number to which this socket is bound is: " + i);
}
} |
Output of the program:-
| Port number to which this socket is bound is: 45974 |
|