Distributed Programming in Java - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Distributed Programming in Java

Description:

There are two flavors of sockets: datagram (UDP) and stream (TCP) sockets. Datagram Sockets ... Similar to DataInput(Output)Stream ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 29
Provided by: peopleScs
Category:

less

Transcript and Presenter's Notes

Title: Distributed Programming in Java


1
Distributed Programming in Java
  • Networking (1)

2
Motivating Scenario
apps
apps
apps
apps
apps
apps
3
nslookup
A program for looking up IP addresses
4
InetAddress
  • This class represents an IP address
  • Each host on an IP network has a host name and a
    numeric address.

5
NsLookup
  • Simple Java version of nslookup

6
Sockets
  • The java.net package provides an object-oriented
    framework for using sockets
  • Sockets offer a fast and flexible way for the
    communication between objects
  • The Socket class represents an endpoint of a
    connection between two processes
  • There are two flavors of sockets datagram (UDP)
    and stream (TCP) sockets

7
Datagram Sockets
  • Datagram sockets use the Unreliable Datagram
    Protocol (UDP)
  • Low-level connectionless
  • No guarantee about packet delivery
  • Neither is their order guaranteed
  • Packets transmitted by the sender either make it
    to the receiver or not
  • Typically used in bandwidth-limited applications
    (e.g. real-time audio, webTV)

8
Stream Sockets
  • Stream sockets use the Transmission Control
    Protocol (TCP)
  • Reliable connection-oriented
  • Packets are guaranteed to be delivered
  • Packets are delivered in order
  • Packets not received within a set time are
    assumed lost and requested again
  • Typically used when lost packets cannot be
    tolerated (e.g. message passing)

9
Socket
  • To create a client-side Socket to connect to a
    specific host and port number
  • To create a server-side Socket to listen on a
    ServerSocket for a connection request

10
Client Socket
  • Key Socket methods

11
ServerSocket
  • Key ServerSocket methods

12
NameServer
  • Simple name server which replies to a name with a
    hostNameport or unknown0
  • The server creates a ServerSocket, then waits in
    a loop to accept() connections, and processes the
    name lookup request

13
NameServer
14
NameServer
15
NameServer
16
Notes
  • Following good practice we make the PORT on which
    the server listens a constant
  • The classes DataInputStream and DataOutStream are
    filter streams that wrap around an input and
    output stream
  • These streams read and write primitive Java
    types readInt(), writeInt(), etc.
  • Strings are Unicode, which results in the
    methods readUTF(), and writeUTF()

17
Name
18
Streams
  • The java.io package allows us to exchange
    formatted data over connections
  • Streams support reading data serially from a
    source, or writing to a destination

19
Byte Streams
20
Character Streams
21
Serialization
  • Object serialization is a process through which
    objects are flattened into a form that can be
    written to and read from a stream
  • Flattened objects can be written to a file or
    even sent across a network connection
  • Supported by the java.io package
  • Key component of the implementation of
    distributed object frameworks in Java

22
Serializable
  • Serialization is almost automatic in Java
  • To use serialization, your objects need to
    implement the Serializable interface
  • This is an example of a marking interface ...
  • ... it does not have any methods
  • ... but indicates (to the JVM) this class is
    designed to be serialized
  • However, the internal machinery required makes
    serialization complex and costly

23
Example Serializable
  • Initial declaration of TestObject

24
Object Streams
  • Objects can be ...
  • serialized with an ObjectOutputStream
  • deserialized with an ObjectInputStream
  • Similar to DataInput(Output)Stream
  • Methods for writing and reading binary
    representations of primitive types
  • But they add the ability to write and read
    non-primitive objects and array values

25
Object Streams
26
Not All Objects (and Data) Should be Serialized
  • Inherently platform-specific objects
  • e.g. FileDescriptors
  • Redundant objects (eg that cache values)
  • Only objects that implement Serializable can be
    serialized (must skip others)
  • Can prevent fields from being serialized by
    declaring them as transient

27
Example Serializable
  • Revised declaration of TestObject
  • both name and panel are now transient
  • assumes that name is derived from value in this
    case (for sake of the example)

28
References
  • Online chapter on network streams
  • Harold, Java I/O, Chapter 5
  • http//www.oreilly.com/catalog/javaio/chapter/ch05
    .html
  • More on using the serialization API
  • Greanier, Flatten Your Objects
  • http//www.javaworld.com/javaworld/jw-07-2000/jw-
    0714-flatten.html
Write a Comment
User Comments (0)
About PowerShow.com