DatagramSocket receives DatagramPacket : DatagramSocket « Network Protocol « Java






DatagramSocket receives DatagramPacket

 

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

public class Main {
  public static void main(String[] args) throws Exception {
    DatagramSocket ds = new DatagramSocket(3000);
    byte[] buf = new byte[1024];
    DatagramPacket dp = new DatagramPacket(buf, 1024);
    ds.receive(dp);
    String strRecv = new String(dp.getData(), 0, dp.getLength()) + " from "
        + dp.getAddress().getHostAddress() + ":" + dp.getPort();
    System.out.println(strRecv);
    ds.close();
  }
}

   
  








Related examples in the same category

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