Java DatagramSocket.getChannel()
Syntax
DatagramSocket.getChannel() has the following syntax.
public DatagramChannel getChannel()
Example
In the following code shows how to use DatagramSocket.getChannel() method.
// w ww . ja va 2 s . c om
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.DatagramChannel;
public class Main {
public static void main(String args[]) {
try {
InetAddress ia = InetAddress.getByName("www.java2s.com");
DatagramSocket ds = new DatagramSocket(8080,ia);
byte buffer[] = "hello".getBytes();
DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);
ds.connect(InetSocketAddress.createUnresolved("google.com", 8080));
DatagramChannel channel = ds.getChannel();
ds.send(dp);
ds.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}