More on Network Programming in Java - PowerPoint PPT Presentation

About This Presentation
Title:

More on Network Programming in Java

Description:

It is an error to declare a class Serializable if this is not the case. ... import java.net.*; public class ChangeMaker { static final public int portNumber = 3339; ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 11
Provided by: markf91
Learn more at: http://www.cs.uni.edu
Category:
Tags: case | java | more | net | network | programming

less

Transcript and Presenter's Notes

Title: More on Network Programming in Java


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(portNumb
    er) 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(so
    ck.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(so
    ck.getOutputStream())
  • Client
  • ObjectInputStream in new ObjectInputStream(sock.
    getInputStream())
  • System.out.println("After ObjectInputStream
    creation") / never printed /
  • ObjectOutputStream out new ObjectOutputStream(so
    ck.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 whats the problem???
  • So whats 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(portNumb
    er) 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.
Write a Comment
User Comments (0)
About PowerShow.com