Interprocess Communication - PowerPoint PPT Presentation

Loading...

PPT – Interprocess Communication PowerPoint presentation | free to view - id: 14a1d0-MzJmZ



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Interprocess Communication

Description:

catch (IOException e){System.out.println('IO:' e.getMessage ... catch(IOException e) {System.out.println('Connection:' e.getMessage());} public void run ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 19
Provided by: jiey7
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Interprocess Communication


1
Interprocess Communication
  • Middleware layers
  • Java API for Internet Protocols
  • UDP Servers
  • TCP Servers
  • Group communication

2
Middleware Layers
Applications, services
RMI and RPC
Middleware
request-reply protocol
layers
marshalling and external data representation
UDP and TCP
3
Sockets and Ports
  • Both UDP and TCP use sockets.
  • Messages sent to a particular Internet address
    and port number.
  • Any process may make use of multiple ports to
    receive messages.

4
Datagram packet
5
The DatagramPacket class in Java
Datagram packet
Array of bytes containing message
Length of message
Internet address
Port number
  • Datagram packets are used to implement a
    connectionless packet delivery service. Each
    message is routed from one machine to another
    based solely on information contained within that
    packet. Multiple packets sent from one machine to
    another might be routed differently, and might
    arrive in any order. Packet delivery is not
    guaranteed.

6
The DatagramSocket class in Java
  • It supports sockets for sending and receiving UDP
    messages (instances of DatagramPacket).
  • It provides a constructor that takes a port
    number as argument.
  • The send and receive methods are for transmitting
    datagrams between a pair of sockets.
  • setSoTimeout it allows a timeout to be set.
  • connect it is used for connecting to a
    particular Internet address and port, in which
    case the socket is only able to send messages to
    and receive messages from that address.

7
sending UDP Datagrams in Java
import java.net. import java.io. public class
UDPClient public static void main(String
args) // args give message contents and
server hostname DatagramSocket aSocket null
try aSocket new DatagramSocket()
byte m args0.getBytes() InetAddress
aHost InetAddress.getByName(args1) int
serverPort 6789
DatagramPacket request
new DatagramPacket(m, args0.length(), aHost,
serverPort) aSocket.send(request)
byte buffer new
byte1000 DatagramPacket reply new
DatagramPacket(buffer, buffer.length) aSocket.
receive(reply) System.out.println("Reply "
new String(reply.getData())) catch
(SocketException e)System.out.println("Socket "
e.getMessage()) catch (IOException
e)System.out.println("IO " e.getMessage())
finally if(aSocket ! null) aSocket.close()

8
receiving UDP Datagrams in Java
import java.net. import java.io. public class
UDPServer public static void main(String
args) DatagramSocket aSocket null
try aSocket new DatagramSocket(6789) b
yte buffer new byte1000 while(true)
DatagramPacket request new
DatagramPacket(buffer, buffer.length)
aSocket.receive(request)
DatagramPacket reply new DatagramPacket(request.
getData(), request.getLength(),
request.getAddress(), request.getPort())
aSocket.send(reply) catch
(SocketException e)System.out.println("Socket "
e.getMessage()) catch (IOException e)
System.out.println("IO " e.getMessage()) f
inally if(aSocket ! null) aSocket.close()

9
The Socket class in Java
  • This class is for use by a pair of processes with
    a connection.
  • The client uses a constructor to create a socket,
    specifying the DNS hostname and port of a server.
  • This constructor not only creates a socket
    associated with a local port but also connects it
    to the specified remote computer and port number.
  • Socket(InetAddress address, int port,
    InetAddress localAddr, int localPort)
  • getInputStream() Returns an input stream for
    this socket.
  • getOutputStream()           Returns an output
    stream for this socket.

10
The ServerSocket class in Java
  • This class implements server sockets. A server
    socket waits for requests to come in over the
    network. It performs some operation based on that
    request, and then possibly returns a result to
    the requester.
  • accept()Listens for a connection to be made to
    this socket and accepts it. The result of
    executing accept is an instance of Socket.

11
TCP client in Java
import java.net. import java.io. public class
TCPClient public static void main (String
args) // arguments supply message and
hostname of destination Socket s null try
int serverPort 7896 s new
Socket(args1, serverPort)
DataInputStream in new DataInputStream(
s.getInputStream()) DataOutputStream out new
DataOutputStream( s.getOutputStream()) out.writ
eUTF(args0) String data in.readUTF()
System.out.println("Received " data)
catch (UnknownHostException
e) System.out.println("Sock"e.getMessage())
catch (EOFException e)System.out.println(
"EOF"e.getMessage()) catch
(IOException e)System.out.println("IO"e.getMess
age()) finally if(s!null) try
s.close()catch (IOException e)System.out.prin
tln("close"e.getMessage())
12
TCP server in Java (1)
import java.net. import java.io. public class
TCPServer public static void main (String
args) try int serverPort 7896
ServerSocket listenSocket new
ServerSocket(serverPort) while(true)
Socket clientSocket listenSocket.accept()
Connection c new Connection(clientSocket)
catch(IOException e) System.out.println("L
isten "e.getMessage())
13
TCP server in Java (2)
class Connection extends Thread
DataInputStream in DataOutputStream
out Socket clientSocket public Connection
(Socket aClientSocket) try
clientSocket aClientSocket in new
DataInputStream( clientSocket.getInputStream())
out new DataOutputStream( clientSocket.getOutput
Stream()) this.start()
catch(IOException e) System.out.println("Connect
ion"e.getMessage()) public void run()
try // an echo
server String data in.readUTF()
out.writeUTF(data)
catch(EOFException e) System.out.println("EOF"e
.getMessage()) catch(IOException e)
System.out.println("IO"e.getMessage())
finally try clientSocket.close()catch
(IOException e)/close failed/
14
Group communication (IP multicast)
  • IP Multicast is built on top of the Internet
    Protocol, IP.
  • IP packets are addressed to computersports
    belongs to the TCP and UDP levels.
  • IP multicast allows the sender to transmit a
    single IP packet to a set of computers that form
    a multicast group.
  • The sender is unaware of the identities of the
    individual recipients and of the size of the
    group.
  • A multicast group is specified by a class D
    Internet address (whose first bits are 1110).

15
The MulticastSocket class in Java (1)
  • The multicast datagram socket class is useful for
    sending and receiving IP multicast packets. A
    MulticastSocket is a (UDP) DatagramSocket, with
    additional capabilities for joining "groups" of
    other multicast hosts on the internet.
  • A multicast group is specified by a class D IP
    address and by a standard UDP port number. Class
    D IP addresses are in the range 224.0.0.0 to
    239.255.255.255, inclusive. The address 224.0.0.0
    is reserved and should not be used.
  • One would join a multicast group by first
    creating a MulticastSocket with the desired port,
    then invoking the joinGroup(InetAddress
    groupAddr) method.

16
The MulticastSocket class in Java (2)
  • When one sends a message to a multicast group,
    all subscribing recipients to that host and port
    receive the message. The socket needn't be a
    member of the multicast group to send messages to
    it.
  • When a socket subscribes to a multicast
    group/port, it receives datagrams sent by other
    hosts to the group/port, as do all other members
    of the group and port. A socket relinquishes
    membership in a group by the leaveGroup(InetAddres
    s addr) method. Multiple MulticastSocket's may
    subscribe to a multicast group and port
    concurrently, and they will all receive group
    datagrams.

17
An example multicast peer joins a group and
sends and receives datagrams (1)
import java.net. import java.io. public class
MulticastPeer public static void main(String
args) // args give message contents
destination multicast group //(e.g.
"228.5.6.7") MulticastSocket s null try
InetAddress group InetAddress.getByName(a
rgs1) s new MulticastSocket(6789)
s.joinGroup(group) byte m
args0.getBytes() DatagramPacket
messageOut new DatagramPacket(m, m.length,
group, 6789) s.send(messageOut)
18
An example multicast peer joins a group and
sends and receives datagrams (2)
  • // get messages from others in group
  • byte buffer new byte1000
  • for(int i0 ilt 3 i)
  • DatagramPacket messageIn
  • new DatagramPacket(buffer, buffer.length)
  • s.receive(messageIn)
  • System.out.println("Received" new
    String(messageIn.getData()))
  • s.leaveGroup(group)
  • catch (SocketException
    e)System.out.println("Socket "
    e.getMessage())
  • catch (IOException e)System.out.println("IO
    " e.getMessage())
  • finally if(s ! null) s.close()
About PowerShow.com