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[] argv) throws Exception {
    byte[] inbuf = new byte[256]; // default size
    DatagramSocket socket = new DatagramSocket();

    DatagramPacket packet = new DatagramPacket(inbuf, inbuf.length);
    socket.receive(packet);//from ww  w . j a  v a  2 s  .com

    int numBytesReceived = packet.getLength();
    System.out.println(numBytesReceived);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    byte[] outbuf = new byte[1024];
    int port = 1234;
    DatagramSocket socket = new DatagramSocket();
    InetAddress groupAddr = InetAddress.getByName("8.1.2.3");
    DatagramPacket packet = new DatagramPacket(outbuf, outbuf.length, groupAddr, port);
    socket.send(packet);/*from  w  w  w .ja va  2  s  . com*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    int mcPort = 12345;
    String mcIPStr = "230.1.1.1";
    DatagramSocket udpSocket = new DatagramSocket();

    InetAddress mcIPAddress = InetAddress.getByName(mcIPStr);
    byte[] msg = "Hello".getBytes();
    DatagramPacket packet = new DatagramPacket(msg, msg.length);
    packet.setAddress(mcIPAddress);/*from  w ww  .ja  v  a 2 s .  com*/
    packet.setPort(mcPort);
    udpSocket.send(packet);

    System.out.println("Sent a  multicast message.");
    System.out.println("Exiting application");
    udpSocket.close();
}

From source file:Main.java

public static void main(String args[]) {
    try {//w  ww  .ja  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.setSoTimeout(1000);
        ds.send(dp);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {/*from w  w w  .  j  a v a 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);
        ds.setSendBufferSize(1000);
        ds.send(dp);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {//from   w ww.  j  a v a  2s .  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);
        ds.setReuseAddress(true);
        ds.send(dp);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {/*ww  w.  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);
        ds.setBroadcast(true);
        ds.send(dp);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {/*from   w  w  w  .  j  a 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);
        ds.setReceiveBufferSize(1000);
        ds.send(dp);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {/*  ww  w  .  j  a  v  a 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);

        ds.send(dp);
        ds.disconnect();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {/*from www.ja  v  a  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);
    } catch (Exception e) {
        e.printStackTrace();
    }
}