September 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Serializing an Object via a Client/Server Connection

  • April 6, 2006
  • By Matt Weisfeld
  • Send Email »
  • More Articles »

Reading the Employee Object and Writing it Back

Now, the infrastructure is in place to read the Employee object off of the "pipe." Once you execute the accept() method, the Server will wait until it is notified that an object is ready to be read. When this happens, the Server continues and the following code is executed.

employee = (Employee )serverInputStream.readObject();

You use the readObject() method to read the object off of the "pipe." This method returns the reference to the Employee object that was sent from the Client. You use the reference that was created earlier. Note that you cast the object to the reference. The readObject() method returns an Object off the "pipe;" you must cast it to the specific object that you want.

Once you retrieve the Employee object from the connection, you can make a couple of simple changes. you do this so that when you send it back to the Client, you can verify that the Server actually changed it.

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

After you change the object, you send it back to the Client using the output stream with the following code:

serverOutputStream.writeObject(employee);

Closing the Object Streams

Finally, you need to do some housekeeping. In this case, you will close both the input and output streams with the following lines.

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

This completes the code for the Server. As with the Client, the code in the Server is incorporated inside a try/catch block. This is required for compilation. As stated earlier, you will cover the various exceptions and how to handle them in later articles. Listing 3 contains the complete code for the Server.

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

public class Server {

   public static void main(String[] arg) {

      Employee employee = null;

      try {

         ServerSocket socketConnection = new ServerSocket(11111);

         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();


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

}

Listing 3: The Server

Running the System

In this article, you simply set up the infrastructure for a working Client/Server model. There are many fascinating points that you will explore in future articles. However, you should get this basic model up and running so that you can verify that the system is in working order.

Compiling the Code

I compile the code using a DOS Shell. Eventually, you 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.

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.

Figure 2: Starting the Server

If everything is working properly, the "Server Waiting" message is displayed. At this point, you 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

Conclusion

In this article, you covered the basic concepts involved with creating a simple Client/Server model. The code is a complete and functional model. There are many interesting aspects of Client/Server models that you have yet to explore.

Moving an object from one place to another is often a tricky proposition. Using modern object-oriented languages makes life a lot easier for today's programmers. It is very interesting to look at code from earlier programming languages pertaining to network programming. In years past, basic network programming required the programmer to develop code that was sometimes quite complex or purchase third-party software that was often hard to use. Languages such as Java and .NET have much of this functionality built right into the language.

As stated earlier, although the example of this article is complete and useful, it is quite basic. There are many more fascinating topics to explore regarding client/server applications. Next month, you will investigate several of these interesting topics.

References

Tyma, Paul, Gabriel Torok and Troy Downing: Java Primer Plus. The Waite Group, 1996.

www.javasoft.com

About the Author

Matt Weisfeld is a faculty member at Cuyahoga Community College (Tri-C) in Cleveland, Ohio. Matt is a member of the Information Technology department, teaching programming languages such as C++, Java, and C# .NET as well as various web technologies. Prior to joining Tri-C, Matt spent 20 years in the information technology industry gaining experience in software development, project management, business development, corporate training, and part-time teaching. Matt holds an MS in computer science and an MBA in project management. Besides The Object-Oriented Thought Process
, which is now in it's second edition, Matt has published two other computer books, and more than a dozen articles in magazines and journals such as Dr. Dobb's Journal, The C/C++ Users Journal, Software Development Magazine, Java Report, and the international journal Project Management. Matt has presented at conferences throughout the United States and Canada.





Page 3 of 3



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel