Example usage for java.net DatagramSocket DatagramSocket

List of usage examples for java.net DatagramSocket DatagramSocket

Introduction

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

Prototype

public DatagramSocket() throws SocketException 

Source Link

Document

Constructs a datagram socket and binds it to any available port on the local host machine.

Usage

From source file:Main.java

public static void main(String args[]) {
    try {/*from  ww  w .j  av  a  2  s.  co m*/

        InetAddress ia = InetAddress.getByName("www.java2s.com");

        DatagramSocket ds = new DatagramSocket();

        byte buffer[] = "hello".getBytes();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);

        // Send the datagram packet
        ds.send(dp);

        System.out.println(ds.getReuseAddress());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

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

        InetAddress ia = InetAddress.getByName("www.java2s.com");

        DatagramSocket ds = new DatagramSocket();

        byte buffer[] = "hello".getBytes();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);

        // Send the datagram packet
        ds.send(dp);

        System.out.println(ds.getReceiveBufferSize());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {/*from   www.jav  a2 s.  c  o m*/

        InetAddress ia = InetAddress.getByName("www.java2s.com");

        DatagramSocket ds = new DatagramSocket();

        byte buffer[] = "hello".getBytes();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);

        // Send the datagram packet
        ds.send(dp);

        System.out.println(ds.getLocalSocketAddress());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {/*from w ww .  j  a  va  2s  .c  o m*/

        InetAddress ia = InetAddress.getByName("www.java2s.com");

        DatagramSocket ds = new DatagramSocket();

        byte buffer[] = "hello".getBytes();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);

        // Send the datagram packet
        ds.send(dp);

        System.out.println(ds.getPort());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {/*from w w w.j a  v  a2s.c  o m*/

        InetAddress ia = InetAddress.getByName("www.java2s.com");

        DatagramSocket ds = new DatagramSocket();

        byte buffer[] = "hello".getBytes();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);

        // Send the datagram packet
        ds.send(dp);

        System.out.println(ds.getTrafficClass());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {//from   w  ww  .  j a va 2s  . c  om

        InetAddress ia = InetAddress.getByName("www.java2s.com");

        DatagramSocket ds = new DatagramSocket();

        byte buffer[] = "hello".getBytes();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);

        // Send the datagram packet
        ds.send(dp);

        System.out.println(ds.getLocalAddress());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {//w  ww .  ja va  2 s. c o m

        InetAddress ia = InetAddress.getByName("www.java2s.com");

        DatagramSocket ds = new DatagramSocket();

        byte buffer[] = "hello".getBytes();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);

        // Send the datagram packet
        ds.send(dp);

        System.out.println(ds.getLocalPort());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

License:asdf

public static void main(String args[]) throws Exception {
    InetAddress ia = InetAddress.getByName(args[0]);
    int port = Integer.parseInt(args[1]);
    DatagramSocket ds = new DatagramSocket();
    while (true) {
        String s = "asdf";
        byte buffer[] = s.getBytes();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, port);
        ds.send(dp);/*from w w  w .j a  v a 2s. c  o  m*/
    }
}

From source file:GetDate.java

public static void main(String args[]) throws Exception {
    byte msg[] = new byte[256];
    DatagramSocket dgSocket = new DatagramSocket();
    InetAddress destination = InetAddress.getByName("web.mit.edu");
    DatagramPacket datagram = new DatagramPacket(msg, msg.length, destination, PortDayTime);
    dgSocket.send(datagram);/*from w w w  .j a v a 2s.c o  m*/
    datagram = new DatagramPacket(msg, msg.length);
    dgSocket.receive(datagram);
    String received = new String(datagram.getData());
    System.out.println("The time in Cambridge is now: " + received);
    dgSocket.close();
}

From source file:Main.java

public static void main(String args[]) {
    try {//from   www  .  j a  va2s  .  c o  m

        InetAddress ia = InetAddress.getByName("www.java2s.com");

        DatagramSocket ds = new DatagramSocket();

        byte buffer[] = "hello".getBytes();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);
        ds.bind(InetSocketAddress.createUnresolved("google.com", 8080));
        ds.send(dp);
        ds.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}