July 25, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Objects and Client/Server Connections

  • May 8, 2006
  • By Matt Weisfeld
  • Send Email »
  • More Articles »

Compiling and Running the System

Compiling the Code

I compiled the code using a DOS Shell. On certain machines it is called a DOS Shell and on others it is called a Command Prompt. They and equivalent and I will show examples of both. Eventually we will need two of these DOS Shells, one to run the Server and one for the Client. You can open a DOS Shell in the Programs->Accessories option.

Type the following code at the command prompt to compile all three of the files.

"C:Program FilesJavajdk1.5.0_06binjavac" Employee.java
"C:Program FilesJavajdk1.5.0_06binjavac" Client.java
"C:Program FilesJavajdk1.5.0_06binjavac" Server.java

Figure 1 shows the screen shot of how this is accomplished.



Click here for a larger image.

Figure 1. Compiling the Code

Starting the Server

In one of the DOS Shells, type in the following line at the command prompt:

"C:Program FilesJavajdk1.5.0_06binjava" Server

Figure 2 shows what happens in the DOS Shell.



Click here for a larger image.

Figure 2 - Starting the Server

If everything is working properly, the "Server Waiting" message is displayed. At this point, we can start the Client.

Starting the Client

In a separate DOS Shell, start the Client with the following line.

"C:Program FilesJavajdk1.5.0_06binjava" Client

The result is shown in Figure 3.

Figure 3 - Starting the Client

If all is well, you will see that the employeeNumber and the employeeName both were changed. You can put some specific identification in the print statements to provide further assurance.

With the circuit complete, the Server should exit cleanly, as shown in Figure 4.

Figure 4 - Completing the System

Resetting the Server

What we have created is a very simple client/server application using the Java programming language. At this point, our server accepts a single transaction from the client and then simply terminates. While this is fine for demonstration purposes, it is not very useful. One of the more interesting exercises that I use in the classroom has multiple clients connecting to the same server. For this to occur, the server obviously must not terminate after serving a single transaction.

Handling Multiple Clients

At a very basic level, there is a simple code enhancement that will allow the Server to handle multiple clients. In fact, the server can also handle multiple transactions from the same client. For example, by adding a simple while loop, we can provide this behavior. Listing 4 contains this enhancement.

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

public class Server {

   public static void main(String[] arg) {

         Employee employee = null;

         try {

            ServerSocket socketConnection = new ServerSocket(11111);

	while (true) {

            	System.out.println("Server Waiting");

            	Socket pipe = socketConnection.accept();

            	ObjectInputStream serverInputStream = new    
                 		ObjectInputStream(pipe.getInputStream());

           	 	ObjectOutputStream serverOutputStream = new 
                 		ObjectOutputStream(pipe.getOutputStream());

            	employee = (Employee )serverInputStream.readObject();

            	employee .setEmployeeNumber(256);
            	employee .setEmployeeName("John");
		
            	serverOutputStream.writeObject(employee);

            	serverInputStream.close();
            	serverOutputStream.close();

	} // end of the while loop

         }  catch(Exception e) {System.out.println(e); 
         }
   }

}

Listing 4: Resetting the Server

The two lines in bold represent the code required to implement the loop. Note that several lines of the code must reside within the loop.

Note: In a later article we will explore some of the performance issues that we can address in a client/server environment.

The basic concept here is that once the Server handles a single client transaction, it closes the input/output stream and then loops back to wait for the next client transaction. Each time the following line is encountered, the Server simply waits.

            	Socket pipe = socketConnection.accept();

If no client is sending transactions, the Server blocks and waits to be notified.

The Sun documentation states the following regarding the ServerSocket accept method:

Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made. A new Socket s is created and, if there is a security manager, the security manager's checkAccept method is called with s.getInetAddress().getHostAddress() and s.getPort() as its arguments to ensure the operation is allowed. This could result in a SecurityException.

To test the use of multiple clients, simply create more than one client. Make sure that each client uses the IP address of the server and you are ready to test. Figure 5 shows how my system looks when I am running the Server with 2 different clients.

Figure 5 - Multiple Clients





Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel