<< 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

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
cell is the smallest unit of the humanity biologically
Abraham
what is biology
Victoria Reply
what is biology
Abraham
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
Biology is a branch of Natural science which deals/About living Organism.
Ahmedin Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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