Java TCP Client Applications
Java removes much of the complexity of writing Network aware applications, by providing a
Socket class (
java.net.Socket). The socket is the software abstraction, used to represent the terminals of a connection between two machines. The basic concept is that you have two sockets on two machines and you create a connection between these sockets, which you use to transfer data. We don't care how the data gets from one socket to another, even though it might have to traverse many network hardware types (hence the abstraction).
We are going to write a finger client to demonstrate how TCP works in Java.
Finger is a standard service that allows a remote machine to query a server and ask it for information on a particular user, or on the users that are logged on. Most UNIX systems support finger, however on many server systems it is disabled, as it provides a little bit too much information for potential 'hackers'.
The host server is usually located at port 79, where a request is usually made up of a username followed by the '\n' character. The server sends any information back to the client and then terminates the connection.
Here is the source for
Finger.java that connects to any finger server and receives the output:
When the Finger application is run it expects the name of a user@host, either specified by the full Internet name, or the name of a machine on the same network segment. If you forget to provide the parameters, it prompts you for the correct parameters (See Figure 7.3, “Finger Client Example”)
Figure 7.3. Finger Client Example
I have run the finger application three times in Figure 7.3, “Finger Client Example”. The first time I omitted the parameter by typing java Finger and I was then prompted for the correct parameter. In the second case I typed java Finger firstname.lastname@example.org and this searches for the username
molloyd at the hostname
khumbu.eeng.dcu.ie (a Unix server in the school with a finger server installed) and this returns details about this user. In the third case I omitted the username and just typed java Finger @khumbu.eeng.dcu.ie. The finger server is designed in this case to return a list of users that are currently logged on, and the locations where they are logged on from. Note: Khumbu may not accept connections from clients outside my subnet for security reasons. So the process that occurs to get the output as in Figure 7.3, “Finger Client Example” is summarized in Figure 7.4, “Finger Client/Server Process”
Figure 10.4. Finger Client/Server Process
The Finger application consists of three main steps:
Figure 7.4 Executing the Finger Client Application (Executed in 2013)
Another example of a client application is a basic web browser. This application allows you to connect to any web server (depending on your proxy) and request a web page. The result is returned in HTML and displayed in the text area as shown below
Figure 7.5. The Basic Web Browser Application
This application connects to a web site on port 80 and sends the string:
GET /index.html \n\n
index.html is the page entered in the visual interface. It then reads the response from the server and outputs to the text area.
Here is the source for
BasicWebBrowser.java that connects to any web server and receives the HTML response.