Example usage for java.net DatagramSocket getChannel

List of usage examples for java.net DatagramSocket getChannel

Introduction

In this page you can find the example usage for java.net DatagramSocket getChannel.

Prototype

public DatagramChannel getChannel() 

Source Link

Document

Returns the unique java.nio.channels.DatagramChannel object associated with this datagram socket, if any.

Usage

From source file:Main.java

public static void main(String args[]) {
    try {//ww  w . j  a v a2  s.co m

        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();
    }
}