Example usage for java.net DatagramSocket receive

List of usage examples for java.net DatagramSocket receive

Introduction

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

Prototype

public synchronized void receive(DatagramPacket p) throws IOException 

Source Link

Document

Receives a datagram packet from this socket.

Usage

From source file:ChatClient.java

public static void main(String[] args) throws Exception {
        int PORT = 4000;
        byte[] buf = new byte[1000];
        DatagramPacket dgp = new DatagramPacket(buf, buf.length);
        DatagramSocket sk;

        sk = new DatagramSocket(PORT);
        System.out.println("Server started");
        while (true) {
            sk.receive(dgp);
            String rcvd = new String(dgp.getData(), 0, dgp.getLength()) + ", from address: " + dgp.getAddress()
                    + ", port: " + dgp.getPort();
            System.out.println(rcvd);

            BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
            String outMessage = stdin.readLine();
            buf = ("Server say: " + outMessage).getBytes();
            DatagramPacket out = new DatagramPacket(buf, buf.length, dgp.getAddress(), dgp.getPort());
            sk.send(out);/*from   w  ww  .  j  ava  2 s. c  om*/
        }
    }

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

        System.out.println(new String(dp.getData()));
    }/*from  www.java  2 s.  c  o  m*/
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    DatagramSocket socket = new DatagramSocket(DAYTIME_PORT);
    while (true) {
        byte buffer[] = new byte[256];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
        socket.receive(packet);
        String date = new Date().toString();
        buffer = date.getBytes();//from ww  w .j  ava 2 s .  co  m
        // Get response address/port for client from packet
        InetAddress address = packet.getAddress();
        int port = packet.getPort();
        packet = new DatagramPacket(buffer, buffer.length, address, port);
        socket.send(packet);
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {//from   w  w w . j  a  va 2  s. co 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:Main.java

public static void main(String args[]) {
    try {// ww  w  .j  av 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:Main.java

public static void main(String args[]) {
    try {//from  ww  w .j a  v a 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, 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 .  j a  v a  2  s.  c o  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:Main.java

public static void main(String args[]) {
    try {/*from  www.j  a v a  2  s.c o  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);
            // Display address from the datagram packet
            InetAddress ia = dp.getAddress();
            System.out.println(ia);

            System.out.println(dp.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 a  2s.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);
            // Display address from the datagram packet
            InetAddress ia = dp.getAddress();
            System.out.println(ia);
            // Display port from the datagram packet
            System.out.println(dp.getPort());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

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

        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);
            // Display address from the datagram packet
            InetAddress ia = dp.getAddress();
            System.out.println(ia);

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