DatagramPacket: getData()


import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

import javax.swing.JPanel;

public class MainClass extends JPanel {

  public static void main(String[] args) {
    try {
      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();
    }
  }
}
Home 
  Java Book 
    Networking  

DatagramPacket:
  1. DatagramPacket
  2. new DatagramPacket(byte[] buffer, int bufferLength)
  3. new DatagramPacket(byte[] buf, int length, InetAddress address, int port)
  4. DatagramPacket: getAddress()
  5. DatagramPacket: getData()
  6. DatagramPacket: getPort()
  7. DatagramPacket: getSocketAddress()