Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

6.5 Creating a TCP Client

Having discussed the functionality of the Socket class, we will now examine a complete TCP client. The client we'll look at here is a daytime client, which, as its name suggests, connects to a daytime server to read the current day and time. Establishing a socket connection and reading from it is a fairly simple process, requiring very little code. By default, the daytime service runs on port 13. Not every machine has a daytime server running, but a Unix server would be a good system to run the client against. If you do not have access to a Unix server, code for a TCP daytime server is given in Section 6.7—the client can be run against it.

Code for DaytimeClient

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

// Chapter 6, Listing 1
public class DaytimeClient
{
     public static final int SERVICE_PORT = 13;

     public static void main(String args[])
     {
          // Check for hostname parameter
          if (args.length != 1)
          {
               System.out.println ("Syntax - DaytimeClient host");
               return;
          }

          // Get the hostname of server
          String hostname = args[0];

          try
          {
               // Get a socket to the daytime service
               Socket daytime = new Socket (hostname, 
               SERVICE_PORT);

               System.out.println ("Connection established");

               // Set the socket option just in case server stalls
               daytime.setSoTimeout ( 2000 );

               // Read from the server
               BufferedReader reader = new BufferedReader (
                 new InputStreamReader
                 (daytime.getInputStream()
               ));

               System.out.println ("Results : " + 
               reader.readLine());

               // Close the connection
               daytime.close();
          }
          catch (IOException ioe)
          {
               System.err.println ("Error " + ioe);
          }
     }
}

How DaytimeClient Works

The daytime application is straightforward, and uses concepts discussed earlier in the chapter. A socket is created, an input stream is obtained, and timeouts are enabled in the rare event that a server as simple as daytime fails during a connection. Rather than connecting a filtered stream, a buffered reader is connected to the socket input stream, and the results are displayed to the user. Finally, the client terminates after closing the socket connection. This is about as simple a socket client as you can get—complexity comes from implementing network protocols, not from network-specific coding.

Running DaytimeClient

Running the application is easy. Simply specify the hostname of a machine running the daytime service as a command-line parameter and run it. If you use a nonstandard port for the daytime server (discussed later), remember to change the port number in the client and recompile.

For example, to run the client against a server running on the local machine, the following command would be used:

java DaytimeClient localhost

NOTE

The daytime server must be running, or the client will be unable to establish a connection. If you're using, for example, a Wintel system, instead of Unix, then you'll need to run the DaytimeServer from later in this chapter.

  • + Share This
  • 🔖 Save To Your Account