Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
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);
            socket.receive(packet);
            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);
            socket.send(packet);
        }
    }
}