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();
}
}
}
Related examples in the same category