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(int port) throws SocketException 

Source Link

Document

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

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    DatagramSocket ds = new DatagramSocket(3000);
    byte[] buf = new byte[1024];
    DatagramPacket dp = new DatagramPacket(buf, 1024);
    ds.receive(dp);// w ww . j a v a 2s .  com
    String strRecv = new String(dp.getData(), 0, dp.getLength()) + " from " + dp.getAddress().getHostAddress()
            + ":" + dp.getPort();
    System.out.println(strRecv);
    ds.close();
}

From source file:Main.java

public static void main(String args[]) {
    try {/*from  w  w w  . java2s  . com*/

        int port = 80;

        DatagramSocket ds = new DatagramSocket(port);

        byte buffer[] = new byte[BUFSIZE];

        while (true) {

            DatagramPacket dp = new DatagramPacket(buffer, buffer.length);

            dp.setPort(9090);

        }
    } 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
        int port = 80;

        DatagramSocket ds = new DatagramSocket(port);

        while (true) {
            byte buffer[] = new byte[BUFSIZE];

            DatagramPacket dp = new DatagramPacket(buffer, buffer.length);

            ds.receive(dp);

            String str = new String(dp.getData());

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

From source file:DatagramReceiver.java

public static void main(String args[]) throws Exception {
    int port = Integer.parseInt(args[0]);
    DatagramSocket ds = new DatagramSocket(port);
    byte buffer[] = new byte[BUFSIZE];
    while (true) {
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
        ds.receive(dp);//  w  w w.j  av  a  2s. c o m
        System.out.println(new String(dp.getData()));
    }
}

From source file:Main.java

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

        int port = 80;

        DatagramSocket ds = new DatagramSocket(port);

        byte buffer[] = new byte[BUFSIZE];

        while (true) {

            DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
            // Receive data
            ds.receive(dp);

            dp.setLength(10);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Collector.java

public static void main(String args[]) throws Exception {
    int port = Integer.parseInt(args[0]);
    DatagramSocket ds = new DatagramSocket(port);

    while (true) {
        byte buffer[] = new byte[BUFSIZE];

        DatagramPacket dp = new DatagramPacket(buffer, buffer.length);

        ds.receive(dp);/* w  w w.  j  a  v  a  2 s  . c om*/

        System.out.println(new String(dp.getData()));
    }
}

From source file:Main.java

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

        int port = 80;

        DatagramSocket ds = new DatagramSocket(port);

        while (true) {
            byte buffer[] = new byte[BUFSIZE];

            DatagramPacket dp = new DatagramPacket(buffer, 0, buffer.length);

            ds.receive(dp);

            String str = new String(dp.getData());

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

From source file:Main.java

public static void main(String args[]) {
    try {//from   w  w  w .  ja v  a2 s  .c  om

        int port = 80;

        DatagramSocket ds = new DatagramSocket(port);

        while (true) {
            byte buffer[] = new byte[BUFSIZE];

            DatagramPacket dp = new DatagramPacket(buffer, buffer.length);

            ds.receive(dp);

            String str = new String(dp.getData());

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

From source file:WriteServer.java

public static void main(String args[]) throws Exception {
    int clientPort = 999;

    int buffer_size = 1024;

    byte buffer[] = new byte[buffer_size];
    DatagramSocket ds = new DatagramSocket(clientPort);
    while (true) {
        DatagramPacket p = new DatagramPacket(buffer, buffer.length);
        ds.receive(p);/*from  w  w w .  ja  v  a  2  s  .c o m*/
        System.out.println(new String(p.getData(), 0, p.getLength()));
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DatagramPacket pack = new DatagramPacket(new byte[BUFFERSIZE], BUFFERSIZE);
    DatagramSocket sock = new DatagramSocket(7);
    // echo back everything
    while (true) {
        sock.receive(pack);/* w  w  w. j a  v a2s. co  m*/
        sock.send(pack);
    }
}