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;

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

    public static void main(String args[]) throws IOException {
        String host = args[0];
        byte message[] = new byte[256];
        InetAddress address = InetAddress.getByName(host);
        System.out.println("Checking at: " + address);
        DatagramPacket packet = new DatagramPacket(message, message.length, address, DAYTIME_PORT);
        DatagramSocket socket = new DatagramSocket();
        socket.send(packet);
        packet = new DatagramPacket(message, message.length);
        socket.receive(packet);
        String time = new String(packet.getData());
        System.out.println(time);
        socket.close();
    }
}