Friday, August 13, 2010

How to make a Socket Connection?

This program creates a socket connection to the server of atomic clock in Boulder, Colorado.

import java.io.*;
import java.net.*;
import java.util.*;

public class Main
{
    public static void main(String args[])
    {
        try
        {
            Socket time = new Socket("time-A.timefreq.bldrdoc.gov", 13);
            try
            {
                InputStream inStream = time.getInputStream();
                Scanner in = new Scanner(inStream);

                while(in.hasNext())
                {
                    String line = in.nextLine();
                    System.out.println(line);
                }
            }
            finally
            {
                time.close();
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

No comments:

Post a Comment