<< Chapter < Page Chapter >> Page >

Date/time program

This program implements a client that gets the date and time from the daytime port (13) on a server that supports that port.

This program is even simpler than the previous one, because it isn't necessary to send anything to the server to get the desired result. All that isnecessary to cause the server to send the information is to make the connection.

This program gets and displays the date and time on the server at " localhost ". However, you can modify the program to access another computer in your networkif you choose to do so. It is not likely that you will find a server on the Internet at large that still supports the daytime port, but you can try.

The program also displays the current date and time in Austin, TX (or wherever the program happens to be run) for comparison.

Beginning of the program named Java4660b

As usual, I will explain this program in fragments. A complete listing is provided in Listing 13 .

As shown in Listing 5 , program begins by instantiating a String object containing the name of the server being used to test the program.

Listing 5 - Beginning of the program named Java4660b.
import java.net.*; import java.io.*;import java.util.*; class Sockets04{public static void main(String[] args){String server = "localhost"; int port = 13; //daytime port try{ //Get a socket, connected to the specified server// on the specified port. Socket socket = new Socket(server,port);

This is followed by the declaration and initialization of an int variable identifying the standard daytime port: port 13.

Than the program gets a socket connection to port 13 on the specified server.

Get an input stream

Following this, the program gets an input stream from the socket and wraps it in the reader classes as shown in Listing 6 .

Listing 6 - Get an input stream.
//Get an input stream from the socket BufferedReader inputStream =new BufferedReader(new InputStreamReader( socket.getInputStream()));

This program doesn't need an output stream because the client doesn't send anything to the server. As mentioned earlier, simply connecting is sufficient to trigger the server tosend the date and time.

Read and display incoming data

After the connection is made via the socket and the input stream is ready to use, the client reads a line of incoming text as shown in Listing 7 . This line of text contains the date and time sent by the server.

Listing 7 - Read and display incoming data.
System.out.println("Current time at " + server); System.out.println(inputStream.readLine());System.out.println("Current time in Austin, TX:"); System.out.println(new Date());//Close the socketsocket.close();

Daytime program output

The program displays this line of text, and also gets and displays the date and time on the local system using the Date class for comparison.

Figure 3 shows the output for one run of the program.

Figure 3 - Daytime program output.
Current time at localhost 8:29:56 AM 1/12/2014Current time in Austin, TX: Sun Jan 12 08:29:56 CST 2014

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask