Receive DatagramPacket : DatagramSocket « Network Protocol « Java






Receive DatagramPacket

 

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

class Collector {
  private final static int BUFSIZE = 20;

  public static void main(String args[]) throws Exception {
    int port = Integer.parseInt(args[0]);
    DatagramSocket ds = new DatagramSocket(port);

    while (true) {
      byte buffer[] = new byte[BUFSIZE];

      DatagramPacket dp = new DatagramPacket(buffer, buffer.length);

      ds.receive(dp);

      System.out.println(new String(dp.getData()));
    }
  }
}

   
  








Related examples in the same category

1.Use DatagramSocket to send out and receive DatagramPacket
2.DatagramSocket sends out DatagramPacket
3.DatagramSocket receives DatagramPacket
4.Datagram Sender
5.Sending a Datagram
6.Receiving a Datagram
7.Joining a Multicast Group
8.Receiving from a Multicast Group
9.Sending to a Multicast Group
10.Send a Datagram
11.User Datagram Protocol Programming
12.Read and write with DatagramPacket
13.Send back the response in a DatagramPacket