Serialization is best described by its absence. In the previous section, we passed That is a correct approach, however, things get a little bit more difficult when we want to send objects that consist of a number of states (i.e. is-a-part-of values). Before the advent of serialization we (as the programmer) were required to break an object into its component parts and send them one-by-one over the stream, where they would then be re-constructed one-by-one. So for example, if an object Object serialization makes this task straightforward, as we simply add to the code the fact that the The serialization mechanism used is capable of handling a wide variety of situations. When you serialize an object you save all the states of the object. This even includes states marked If the import java.io.*; // contains the Serializable interface public class Fish implements Serializable { private String name; private String type; public Fish(String name, String type) { this.name = name; this.type = type; } public String getName() { return this.name; } public String getType() { return this.type; } } The On the client-side we can create a fish object (say myFish) and send it using the So, for example: if (o.getClass().getName().equals("Fish")) { Fish aFish = (Fish) o; } Once we have converted to the System.out.println("\nFish name is: "+ aFish.getName()); System.out.println("\nFish type is: "+ aFish.getType()); Once an object is written to a stream, its state is fixed. In effect, a copy of the object is created on the client side and sent to the server side. If this object changes state on the client side, while the server is busy processing the copy, then the copy is no longer up-to-date. This can be problematic. One solution is to lock the client object on the client side, so that it cannot be altered while the copied object is in transit to or from the server - but what happens if the server or communication fails? The use of serialization also allows easy persistent storage through the use of files. For example if we wished to store the FileOutputStream out = new FileOutputStream("Storage.file"); ObjectOutput s = new ObjectOutputStream(out); s.writeObject("Some example string"); // store an example That allows us to store a FileInputStream in = new FileInputStream("Storage.file"); ObjectInputStream s = new ObjectInputStream(in); String theString = (String) s.readObject(); Fish myFish = (Fish) s.readObject(); As the communication becomes more complex, the serialization task becomes even more difficult. Serialization, although providing a huge advantage over manually streaming data, still requires the programmer to maintain the protocol for communication, distributing the protocols and making sure that both the client and server are aware of the serialized object class (in this case Task: Modify the code from the section called “The TCP Client/Server” to send a These notes are copyright Dr. Derek Molloy, School of Electronic Engineering, Dublin City University, Ireland 2013-present. Please contact him directly before reproducing any of the content in any way. |