DatagramPacket: getPort()


import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class MainClass {
  private final static int BUFSIZE = 20;

  public static void main(String args[]) {
    try {

      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);
        // Display port from the datagram packet
        System.out.println(dp.getPort());
      }
    } catch (Exception 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()