Home > Articles > Programming > Java

Network Programming: Creating Clients and Servers with Java Sockets

Larry Brown and Marty Hall examine how to directly implement clients and servers in the Java platform by using network "sockets."
This sample chapter is excerpted from Core Web Programming, by Larry Brown and Marty Hall.
This chapter is from the book

This chapter is from the book

Topics in This Chapter

  • Implementing a generic network client

  • Processing strings with StringTokenizer

Network programming involves two distinct pieces: a client and a server.

Core Note

Remember that the security manager of most browsers prohibits applets from making network connections to machines other than the one from which they are loaded.

In this chapter, you'll see how to directly implement clients and servers by using network "sockets." Although this is the lowest-level type of network programming in the Java platform, if you've used sockets in other languages, you may be surprised at how simple they are to use in Java technology. The URL class helps hide the details of network programming by providing methods for opening connections and binding input/output streams with sockets. In addition to sockets, which can communicate with general-purpose programs in arbitrary languages, Java provides two higher-level packages for communicating with specific types of systems: Remote Method Invocation (RMI) and database connectivity (JDBC). The RMI package lets you easily access methods in remote Java objects and transfer serializable objects across network connections. RMI is covered in Section 17.9. JDBC lets you easily send SQL statements to remote databases. Java Database Connectivity is covered in Chapter 22.

17.1 Implementing a Client

The client is the program that initiates a network connection. Implementing a client consists of five basic steps:

  1. Create a Socket object.

  2. Create an output stream that can be used to send information to the Socket.

  3. Create an input stream to read the response from the server.

  4. Do I/O with input and output streams.

  5. Close the Socket when done.

Each of these steps is described in the sections that follow. Note that most of the methods described in these sections throw an IOException and need to be wrapped in a try/catch block.

Create a Socket object.

A Socket is the Java object corresponding to a network connection. A client connects to an existing server that is listening on a numbered network port for a connection. The standard way of making a socket is to supply a hostname or IP address and port as follows:

Socket client = new Socket("hostname", portNumber);

or

Socket client = new Socket("IP address", portNumber);

If you are already familiar with network programming, note that this approach creates a connection-oriented socket. The Java programming language also supports connectionless (UDP) sockets through the Data_gramSocket class.

Create an output stream that can be used to send information to the Socket.

The Java programming language does not have separate methods to send data to files, sockets, and standard output. Instead, Java starts with different underlying objects, then layers standard output streams on top of them. So, any variety of OutputStream available for files is also available for sockets. A common one is PrintWriter. This stream lets you use print and println on the socket in exactly the same way as you would print to the screen. The PrintWriter constructor takes a generic Out_putStream as an argument, which you can obtain from the Socket by means of getOutputStream. In addition, you should specify true in the constructor to force autoflush. Normally, the contents written to the stream will remain in a buffer until the buffer becomes completely full. Once the buffer is full, the contents are flushed out the stream. Autoflush guarantees that the buffer is flushed after every println, instead of waiting for the buffer to fill. Here's an example:

PrintWriter out = 
 new PrintWriter(client.getOutputStream(), true);

You can also use an ObjectOutputStream to send complex Java objects over the network to be reassembled at the other end. An ObjectOutputStream connected to the network is used in exactly the same way as one connected to a file; simply use writeObject to send a serializable object and all referenced serializable objects. The server on the other end would use an ObjectInputStream's readObject method to reassemble the sent object. Note that all AWT components are automatically serializable, and making other objects serializable is a simple matter of declaring that they implement the Serializ_able interface. See Section 13.9 (Serializing Windows) for more details and an example. Also see Section 17.9 (RMI: Remote Method Invocation) for a high-level interface that uses serialization to let you distribute Java objects across networks.

Create an input stream to read the response from the server.

Once you send data to the server, you will want to read the server's response. Again, there is no socket-specific way of doing this; you use a standard input stream layered on top of the socket. The most common one is an InputStreamReader, for handling character-based data. Here is a sample:

InputStreamReader in = 
 new InputStreamReader(client.getInputStream()); 

Although this approach is the simplest, in most cases a better approach is to wrap the socket's generic InputStream inside a BufferedReader. This approach causes the system to read the data in blocks behind the scenes, rather than reading the underlying stream every time the user performs a read. This approach usually results in significantly improved performance at the cost of a small increase in memory usage (the buffer size, which defaults to 512 bytes). Here's the idea:

BufferedReader in = 
 new BufferedReader
 (new InputStreamReader(client.getInputStream()));

Core Performance Tip

If you are going to read from a socket multiple times, a buffered input stream can speed things up considerably.

In a few cases, you might want to send data to a server but not read anything back. You could imagine a simple e-mail client working this way. In that case, you can skip this step. In other cases, you might want to read data without sending anything first. For instance, you might connect to a network "clock" to read the time. In such a case, you would skip the output stream and just follow this step. In most cases, however, you will want to both send and receive data, so you will follow both steps. Also, if the server is sending complex objects and is written in the Java programming language, you will want to open an ObjectInputStream and use readObject to receive data.

Do I/O with input and output streams.

A PrintStream has print and println methods that let you send a single primitive value, a String, or a string representation of an Object over the network. If you send an Object, the object is converted to a String by calling the toString method of the class. Most likely you are already familiar with these methods, since System.out is in fact an instance of Print_Stream. PrintStream also inherits some simple write methods from OutputStream. These methods let you send binary data by sending an individual byte or an array of bytes.

PrintWriter is similar to PrintStream and has the same print and println methods. The main difference is that you can create print writers for different Unicode character sets, and you can't do that with PrintStream.

BufferedReader has two particularly useful methods: read and readLine. The read method returns a single char (as an int); readLine reads a whole line and returns a String. Both of these methods are blocking; they do not return until data is available. Because readLine will wait until receiving a carriage return or an EOF (the server closed the connection), readLine should be used only when you are sure the server will close the socket when done transmitting or when you know the number of lines that will be sent by the server. The readLine method returns null upon receiving an EOF.

Close the Socket when done.

When you are done, close the socket with the close method:

client.close();

This method closes the associated input and output streams as well.

Example: A Generic Network Client

Listing 17.1 illustrates the approach outlined in the preceding section. Processing starts with the connect method, which initiates the connection, then passes the socket to handleConnection to do the actual communication. This version of handleConnection simply reports who made the connection, sends a single line to the server ("Generic Network Client"), reads and prints a single response line, and exits. Real clients would override handleConnection to implement their desired behavior but could leave connect unchanged.

Listing 17.1 NetworkClient.java

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

/** A starting point for network clients. You'll need to
 * override handleConnection, but in many cases connect can 
 * remain unchanged. It uses SocketUtil to simplify the
 * creation of the PrintWriter and BufferedReader.
 */

public class NetworkClient {
 protected String host;
 protected int port;

 /** Register host and port. The connection won't
  * actually be established until you call
  * connect.
  */
 
 public NetworkClient(String host, int port) {
  this.host = host;
  this.port = port;
 }

 /** Establishes the connection, then passes the socket
  * to handleConnection.
  */
 public void connect() {
  try {
   Socket client = new Socket(host, port);
   handleConnection(client);
  } catch(UnknownHostException uhe) {
   System.out.println("Unknown host: " + host);
   uhe.printStackTrace();
  } catch(IOException ioe) {
   System.out.println("IOException: " + ioe);
   ioe.printStackTrace();
  }
 }

 /** This is the method you will override when
  * making a network client for your task.
  * The default version sends a single line
  * ("Generic Network Client") to the server,
  * reads one line of response, prints it, then exits.
  */
 
 protected void handleConnection(Socket client)
  throws IOException {
  PrintWriter out = SocketUtil.getWriter(client);
  BufferedReader in = SocketUtil.getReader(client);
  out.println("Generic Network Client");
  System.out.println
   ("Generic Network Client:\n" +
    "Made connection to " + host +
    " and got '" + in.readLine() + "' in response");
  client.close();
 }

 /** The hostname of the server we're contacting. */
 
 public String getHost() {
  return(host);
 }

 /** The port connection will be made on. */
 
 public int getPort() {
  return(port);
 }
}

The SocketUtil class is just a simple interface to the BufferedReader and PrintWriter constructors and is given in Listing 17.2.

Listing 17.2 SocketUtil.java

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

/** A shorthand way to create BufferedReaders and
 * PrintWriters associated with a Socket.
 */

public class SocketUtil {
 /** Make a BufferedReader to get incoming data. */

 public static BufferedReader getReader(Socket s)
   throws IOException {
  return(new BufferedReader(
    new InputStreamReader(s.getInputStream())));
 }

 /** Make a PrintWriter to send outgoing data.
  * This PrintWriter will automatically flush stream
  * when println is called.
  */

 public static PrintWriter getWriter(Socket s)
   throws IOException {
  // Second argument of true means autoflush.
  return(new PrintWriter(s.getOutputStream(), true));
 }
}

Finally, the NetworkClientTest class, shown in Listing 17.3, provides a way to use the NetworkClient class with any hostname and any port.

Listing 17.3 NetworkClientTest.java

/** Make simple connection to host and port specified. */

public class NetworkClientTest {
 public static void main(String[] args) {
  String host = "localhost";
  int port = 8088;  
  if (args.length > 0) {
   host = args[0];
  }
  if (args.length > 1) {
   port = Integer.parseInt(args[1]);
  }
  NetworkClient nwClient = new NetworkClient(host, port);
  nwClient.connect();
 }
}

Output: Connecting to an FTP Server

Let's use the test program in Listing 17.3 to connect to Netscape's public FTP server, which listens on port 21. Assume > is the DOS or Unix prompt.

> java NetworkClientTest ftp.netscape.com 21
Generic Network Client:
Made connection to ftp.netscape.com and got '220 ftp26 FTP server 
(UNIX(r) System V Release 4.0) ready.' in response

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020