DatagramSocket Connect() Example
Connects the socket to a remote address for this socket.
DatagramSocketclass Connect() method example. This example shows you how to use Connect() method.This method Connects the socket to a remote address for this socket.
Here is the code:-
/**
* @Program that Connects the socket to a remote address for this socket.
* Connect.java
* Author:-RoseIndia Team
* Date:-20-jun-2008
*/
import java.net.*;
public class Connect {
public static void main(String[] args)throws Exception {
DatagramSocket ds=new DatagramSocket() ;
// Returns the local host.
InetAddress i=InetAddress.getLocalHost();
// Returns the address to which this socket is connected.
System.out.println("address to which this socket is connected: "+ds.getInetAddress());
//Connects the socket to a remote address for this socket.
ds.connect(i, 8080);
System.out.println("address to which this socket is connected: "+ds.getInetAddress());
}
} |
Output of the program:-
address to which this socket is connected: null
address to which this socket is connected: girish-desktop/127.0.1.1 |
|