Example usage for java.net DatagramPacket setData

List of usage examples for java.net DatagramPacket setData

Introduction

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

Prototype

public synchronized void setData(byte[] buf, int offset, int length) 

Source Link

Document

Set the data buffer for this packet.

Usage

From source file:Main.java

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

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

            buffer = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            dp.setData(buffer, 2, 5);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}