1
INTERPROCESS COMMUNICATION
Part-I
Gullagong, Associate Professor
PG Dept. of Computer Sc &IT
INTERPROCESS COMMUNICATION
Topics
2
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
Introduction
3
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
Introduction
4
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
Introduction
5
Figure 1. Middleware layers
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
Introduction
6
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
Introduction
7
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
Introduction
8
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
Introduction
9
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
Introduction
10
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
The API for the Internet Protocols
11
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
The Characteristics of Interprocess Communication
12
SYSTEM MODEL
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
The Characteristics of Interprocess Communication
13
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
The Characteristics of Interprocess Communication
14
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
The Characteristics of Interprocess Communication
15
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
Sockets
16
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
Sockets
17
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
Sockets
18
(Figure 2)
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
Sockets
19
Figure 2. Sockets and ports
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
UDP Datagram Communication
20
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
UDP Datagram Communication
21
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
UDP Datagram Communication
22
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
UDP Datagram Communication
23
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
UDP Datagram Communication
24
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
UDP Datagram Communication
25
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
Java API for UDP Datagrams
26
array of bytes containing message | length of message| Internet address | port number|
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
Java API for UDP Datagrams
27
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
Java API for UDP Datagrams
28
Example
– The process creates a socket, sends a message to a server at port 6789 and waits to receive a reply.
(Figure 3)
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
Java API for UDP Datagrams
29
import java.net.*;
import java.io.*;
public class UDPClient{
public static void main(String args[]){
// args give message contents and destination hostname
try {
DatagramSocket aSocket = new DatagramSocket(); // create socket
byte [] m = args[0].getBytes();
InetAddress aHost = InetAddress.getByName(args[1]); // DNS lookup
int serverPort = 6789;
DatagramPacket request =
new DatagramPacket(m, args[0].length(), aHost, serverPort);
aSocket.send(request); //send nessage
byte[] buffer = new byte[1000];
DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
aSocket.receive(reply); //wait for reply
System.out.println("Reply: " + new String(reply.getData()));
aSocket.close();
}catch (SocketException e){System.out.println("Socket: " + e.getMessage());
}catch (IOException e){System.out.println("IO: " + e.getMessage());}
} finally{if (aSocket !=null)aSocket.close()}
}
}
Figure 3. UDP client sends a message to the server and gets a reply
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
Java API for UDP Datagrams
30
Example
– The process creates a socket, bound to its server port 6789 and waits to receive a request message from a client.
(Figure 4)
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
Java API for UDP datagrams
31
import java.net.*;
import java.io.*;
public class UDPServer{
public static void main(String args[]){
DatagramSocket aSocket = null;
try {
aSocket = new DatagramSocket(6789);
byte []buffer = new byte[1000];
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());}
}finally{if (aSocket !=null)aSocket.close()}
}
}
Figure 4. UDP server repeatedly receives a request and sends it back to the client
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
TCP Stream Communication
32
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
TCP Stream Communication
33
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
TCP Stream Communication
34
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
TCP Stream Communication
35
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
Java API for UDP Datagrams
36
Example
– The client process creates a socket, bound to the hostname and server port 6789.
(Figure 5)
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
Java API for UDP Datagrams
37
Example
– The server process opens a server socket to its server port 6789 and listens for connect requests.
(Figure 6,7)
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
TCP Stream Communication
38
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("Listen socket:"+e.getMessage());}
}
}
Figure 6. TCP server makes a connection for each client and then echoes the client’s request
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION
TCP Stream Communication
39
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.getOutputStream());
this.start();
} catch(IOException e){System.out.println("Connection:"+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("readline:"+e.getMessage());}
} finally {try{clientSocket.close();}catch(IOException e){/*close failed*/}}
}
}
Figure 7. TCP server makes a connection for each client and then echoes the client’s request
Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005
INTERPROCESS COMMUNICATION