Example usage for java.net DatagramPacket DatagramPacket

List of usage examples for java.net DatagramPacket DatagramPacket

Introduction

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

Prototype

public DatagramPacket(byte buf[], int length) 

Source Link

Document

Constructs a DatagramPacket for receiving packets of length length .

Usage

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);// ww w. j a va2 s.c o  m
        sock.send(pack);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    byte[] ary = new byte[128];
    DatagramPacket pack = new DatagramPacket(ary, 128);

    // read/*from w ww .jav  a2  s. c  om*/
    DatagramSocket sock = new DatagramSocket(1111);
    sock.receive(pack);
    String word = new String(pack.getData());
    System.out.println("From: " + pack.getAddress() + " Port: " + pack.getPort());
    System.out.println(word);
    sock.close();
    // write
    sock = new DatagramSocket();
    pack.setAddress(InetAddress.getByName(args[1]));
    pack.setData(args[2].getBytes());
    pack.setPort(1111);
    sock.send(pack);
    sock.close();

}

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);//from w  w w. jav  a2  s.c  o  m
    String strRecv = new String(dp.getData(), 0, dp.getLength()) + " from " + dp.getAddress().getHostAddress()
            + ":" + dp.getPort();
    System.out.println(strRecv);
    ds.close();
}

From source file:PacketReceiver.java

public static void main(String[] args) throws Exception {
    byte[] buffer = new byte[1024];
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
    DatagramSocket socket = new DatagramSocket(5002);
    socket.receive(packet);/*from  ww w  .  j  av a  2s .c  om*/
    System.out.println(packet.getSocketAddress());
    buffer = packet.getData();
    System.out.println(new String(buffer));
}

From source file:Main.java

public static void main(String[] args) {
    try {/*from  ww  w  .  ja  va 2s .  c  o  m*/
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

        DatagramSocket socket = new DatagramSocket(5002);

        System.out.println("Waiting for a packet...");
        socket.receive(packet);

        System.out.println("Just received packet from " + packet.getSocketAddress());
        buffer = packet.getData();

        System.out.println(new String(buffer));

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

From source file:Main.java

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

    MulticastSocket msocket = new MulticastSocket(9999);
    byte[] inbuf = new byte[1024];
    DatagramPacket packet = new DatagramPacket(inbuf, inbuf.length);

    msocket.receive(packet);//  www. j av a  2 s. c om

    // Data is now in inbuf
    int numBytesReceived = packet.getLength();
}

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);/*  w w  w. j  av a  2  s . c  om*/

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

From source file:UdpEchoServer.java

public static void main(String[] args) {
    DatagramSocket sock;/*from w  ww.  j  av  a 2s.  c om*/
    DatagramPacket pack = new DatagramPacket(new byte[BUFFERSIZE], BUFFERSIZE);
    try {
        sock = new DatagramSocket(7);
    } catch (SocketException e) {
        System.out.println(e);
        return;
    }
    // echo back everything
    while (true) {
        try {
            sock.receive(pack);
            sock.send(pack);
        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    }
}

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 ww w  .  j a va 2  s.  com
        System.out.println(new String(p.getData(), 0, p.getLength()));
    }
}

From source file:UDP0.java

public static void main(String[] args) throws Exception {
    byte[] ary = new byte[128];
    DatagramPacket pack = new DatagramPacket(ary, 128);
    if (args[0].charAt(0) == 'r') {
        // read//w  w w  .  ja va  2s.  c o m
        DatagramSocket sock = new DatagramSocket(1111);
        sock.receive(pack);
        String word = new String(pack.getData());
        System.out.println("From: " + pack.getAddress() + " Port: " + pack.getPort());
        System.out.println(word);
        sock.close();
    } else { // write
        DatagramSocket sock = new DatagramSocket();
        pack.setAddress(InetAddress.getByName(args[1]));
        pack.setData(args[2].getBytes());
        pack.setPort(1111);
        sock.send(pack);
        sock.close();
    }
}