Java Reference
In-Depth Information
The TimeServer application makes a connection to any client that connects to port 4415,
displays the current time, and then closes the connection.
For an application to act as a server, it must monitor at least one port on the host
machine for client connections. Port 4415 was chosen arbitrarily for this project, but it
could be any number from 1024 to 65,535.
The Internet Assigned Numbers Authority controls the usage of
ports 0 to 1023, but claims are staked to the higher ports on a
more informal basis. When choosing port numbers for your own
client/server applications, it's a good idea to do research on what
ports are being used by others. Search the Web for references to
the port you want to use and plug the terms “Registered Port
Numbers” and “Well-Known Port Numbers” into search engines to
find lists of in-use ports. A good guide to port usage is available
on the Web at http://www.sockets.com/services.htm.
NOTE
When a client is detected, the server creates a Date object that represents the current date
and time and then sends it to the client as a String .
In this exchange of information between the server and client, the server does almost all
the work. The client's only responsibility is to establish a connection to the server and
display messages received from the server.
Although you could develop a simple client for a project like this, you also can use any
telnet application to act as the client, as long as it can connect to a port you designate.
Windows includes a command-line application called telnet that you can use for this pur-
pose.
Listing 17.3 contains the full source code for the server application.
LISTING 17.3
The Full Text of TimeServer.java
1: import java.io.*;
2: import java.net.*;
3: import java.util.*;
4:
5: public class TimeServer extends Thread {
6: private ServerSocket sock;
7:
8: public TimeServer() {
9: super();
10: try {
 
Search WWH ::




Custom Search