1 / 10

More on Network Programming in Java

More on Network Programming in Java. Chapter 22. Transmitting Objects over the Network. Objects to be transmitted over the network via object serialization The object to be transmitted implements the Serializable interface

Download Presentation

More on Network Programming in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More on Network Programming in Java Chapter 22

  2. Transmitting Objects over the Network • Objects to be transmitted over the network via object serialization • The object to be transmitted implements the Serializable interface • Classes that do not implement this interface will not have any of their state serialized or deserialized. • All subtypes of a serializable class are themselves serializable. • The serialization interface has no methods or fields and serves only to identify the semantics of being serializable. • To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class Serializable if this is not the case. The error will be detected at runtime.

  3. ChangeMaker Server import java.io.*;import java.net.*;public class ChangeMaker { static final public int portNumber = 3339; static final public double exchangeBPenniestoAPennies = 1.615; public ChangeMaker() throws IOException { ServerSocket server = new ServerSocket(portNumber); while (true) { Socket sock = server.accept(); // got client, make the connection System.out.println("After socket creation"); ObjectInputStream in = new ObjectInputStream(sock.getInputStream()); System.out.println("After ObjectInputStream creation"); ObjectOutputStream out = new ObjectOutputStream(sock.getOutputStream()); System.out.println("After ObjectOutputStream creation"); // read the value PocketChange coins; try { coins = (PocketChange) in.readObject(); } catch (ClassNotFoundException e) { continue; } // end try-catch

  4. ChangeClient … Socket sock = new Socket(InetAddress.getLocalHost(), portNumber); System.out.println("After socket creation"); ObjectInputStream in = new ObjectInputStream(sock.getInputStream()); System.out.println("After ObjectInputStream creation"); ObjectOutputStream out = new ObjectOutputStream(sock.getOutputStream()); System.out.println("After ObjectOutputStream creation"); // write out british object out.writeObject(bc); out.flush();

  5. Lab 12 Observed Problem Both Client and Server “hang” when trying to perform “new ObjectInputStream(…)”: Server: ObjectInputStream in = new ObjectInputStream(sock.getInputStream()); System.out.println("After ObjectInputStream creation"); /* never printed */ ObjectOutputStream out = new ObjectOutputStream(sock.getOutputStream()); Client: ObjectInputStream in = new ObjectInputStream(sock.getInputStream()); System.out.println("After ObjectInputStream creation"); /* never printed */ ObjectOutputStream out = new ObjectOutputStream(sock.getOutputStream());

  6. Lab 12 Problem public ObjectInputStream(InputStream in) throws IOException • Creates an ObjectInputStream that reads from the specified InputStream. • A serialization stream header is read from the stream and verified. • This constructor will block until the corresponding ObjectOutputStream has written and flushed the header. • So what’s the problem??? • So what’s the solution???

  7. Lab 12 Problem public ObjectOutputStream(OutputStream out) throws IOException • Creates an ObjectOutputStream that writes to the specified OutputStream. • This constructor writes the serialization stream header to the underlying stream; • callers may wish to flush the stream immediately to ensure that constructors for receiving ObjectInputStreams will not block when reading the header.

  8. This is a common problem in CS • Deadlock – two (or more) programs are waiting indefinitely for an event that can only be caused by another waiting program.

  9. Corrected ChangeMaker Server import java.io.*;import java.net.*;public class ChangeMaker { static final public int portNumber = 3339; static final public double exchangeBPenniestoAPennies = 1.615; public ChangeMaker() throws IOException { ServerSocket server = new ServerSocket(portNumber); while (true) { Socket sock = server.accept(); // got client, make the connection System.out.println("After socket creation"); ObjectOutputStream out = new ObjectOutputStream(sock.getOutputStream()); System.out.println("After ObjectOutputStream creation"); out.flush(); // did not seem to be needed, but a good idea ObjectInputStream in = new ObjectInputStream(sock.getInputStream()); System.out.println("After ObjectInputStream creation"); // read the value PocketChange coins; try { coins = (PocketChange) in.readObject(); } catch (ClassNotFoundException e) { continue; } // end try-catch

  10. To the lab… • Work on completing Part B of Lab 12.

More Related