DatagramSocket Bind() Example
Binds this DatagramSocket to a specific address & port.
DatagramSocketclass Bind() method example. This example shows you how to use Bind() method.This method Binds this DatagramSocket to a specific address & port.
Here is the code:-
/**
* @Program that Returns the address of the endpoint this socket is bound to
* Bind.java
* Author:-RoseIndia Team
* Date:-20-jun-2008
*/
import java.net.*;
public class Bind {
public static void main(String[] args)throws SocketException {
DatagramSocket ds=new DatagramSocket();
// Returns the port number on the local host to which this socket is bound.
System.out.println(ds.getLocalPort());
InetSocketAddress is=new InetSocketAddress("192.168.10.211",9090);
//Returns the address of the endpoint this socket is bound to
ds.bind(is );
System.out.println(ds.getLocalPort());
}
} |
Output of the program:-
44754
Exception in thread "main" java.net.SocketException: already bound
at java.net.DatagramSocket.bind(DatagramSocket.java:355)
at Bind.main(Bind.java:15) |
|