MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

import java.io.InputStream;
import java.net.Socket;

public class MainClass {

    public static void main(String[] args) throws Exception {

        String hostname = "tock.usno.navy.mil";

        Socket theSocket = new Socket(hostname, 13);
        InputStream timeStream = theSocket.getInputStream();
        StringBuffer time = new StringBuffer();
        int c;
        while ((c = timeStream.read()) != -1)
            time.append((char) c);
        String timeString = time.toString().trim();
        System.out.println("It is " + timeString + " at " + hostname);
    }

}