Send back the response in a DatagramPacket : DatagramSocket « Network Protocol « Java

Send back the response in a DatagramPacket


import java.util.Date;

public class Main {
  final private static int DAYTIME_PORT = 13;

  public static void main(String args[]) throws IOException {
    DatagramSocket socket = new DatagramSocket(DAYTIME_PORT);
    while (true) {
      byte buffer[] = new byte[256];
      DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
      String date = new Date().toString();
      buffer = date.getBytes();
      // Get response address/port for client from packet
      InetAddress address = packet.getAddress();
      int port = packet.getPort();
      packet = new DatagramPacket(buffer, buffer.length, address, port);


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.Receive DatagramPacket
11.Send a Datagram
12.User Datagram Protocol Programming
13.Read and write with DatagramPacket