DatagramPacket: getSocketAddress() : DatagramPacket « java.net « Java by API






DatagramPacket: getSocketAddress()

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

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