1 of 39

1

INTERPROCESS COMMUNICATION

Part-I

Gullagong, Associate Professor

PG Dept. of Computer Sc &IT

INTERPROCESS COMMUNICATION

2 of 39

Topics

2

  • INTRODUCTION
  • The API for the INTERNET PROTOCOLS
  • EXTERNAL DATA REPRESENTATION
  • CLIENT-SERVER COMMUNICATION
  • GROUP COMMUNICATION

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

3 of 39

Introduction

3

  • The java API for interprocess communication in the internet provides both datagram and stream communication.
  • The two communication patterns that are most commonly used in distributed programs:
    • Client-Server communication
      • The request and reply messages provide the basis for remote method invocation (RMI) or remote procedure call (RPC).

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

4 of 39

Introduction

4

    • Group communication
      • The same message is sent to several processes.

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

5 of 39

Introduction

5

  • This chapter is concerned with middleware.

Figure 1. Middleware layers

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

6 of 39

Introduction

6

  • Remote Method Invocation (RMI)
    • It allows an object to invoke a method in an object in a remote process.
      • E.g. CORBA and Java RMI
  • Remote Procedure Call (RPC)
    • It allows a client to call a procedure in a remote server.

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

7 of 39

Introduction

7

  • The application program interface �(API) to UDP provides a message passing abstraction.
    • Message passing is the simplest form of interprocess communication.
    • API enables a sending process to transmit a single message to a receiving process.
    • The independent packets containing theses messages are called datagrams.
    • In the Java and UNIX APIs, the sender specifies the destination using a socket.

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

8 of 39

Introduction

8

    • Socket is an indirect reference to a particular port used by the destination process at a destination computer.
  • The application program interface (API) to TCP provides the abstraction of a two-way stream between pairs of processes.

  • The information communicated consists of a stream of data items with no message boundaries.

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

9 of 39

Introduction

9

  • Request-reply protocols are designed to support client-server communication in the form of either RMI or RPC.

  • Group multicast protocols are designed to support group communication.

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

10 of 39

Introduction

10

  • Group multicast is a form of interprocess communication in which one process in a group of processes transmits the same message to all members of the group.

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

11 of 39

The API for the Internet Protocols

11

  • The CHARACTERISTICS of INTERPROCESS COMMUNICATION
  • SOCKET
  • UDP DATAGRAM COMMUNICATION
  • TCP STREAM COMMUNICATION

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

12 of 39

The Characteristics of Interprocess Communication

12

  • Synchronous and asynchronous communication
    • In the synchronous form, both send and receive are blocking operations.
    • In the asynchronous form, the use of the send operation is non-blocking and the receive operation can have blocking and non-blocking variants.

SYSTEM MODEL

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

13 of 39

The Characteristics of Interprocess Communication

13

  • Message destinations
    • A local port is a message destination within a computer, specified as an integer.
    • A port has an exactly one receiver but can have many senders.

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

14 of 39

The Characteristics of Interprocess Communication

14

  • Reliability
    • A reliable communication is defined in terms of validity and integrity.
    • A point-to-point message service is described as reliable if messages are guaranteed to be delivered despite a reasonable number of packets being dropped or lost.
    • For integrity, messages must arrive uncorrupted and without duplication.

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

15 of 39

The Characteristics of Interprocess Communication

15

  • Ordering
    • Some applications require that messages be delivered in sender order.

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

16 of 39

Sockets

16

  • Internet IPC mechanism of Unix and other operating systems (BSD Unix, Solaris, Linux, Windows NT, Macintosh OS)
  • Processes in the above OS can send and receive messages via a socket.
  • Sockets need to be bound to a port number and an internet address in order to send and receive messages.
  • Each socket has a transport protocol (TCP or UDP).

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

17 of 39

Sockets

17

  • Messages sent to some internet address and port number can only be received by a process using a socket that is bound to this address and port number.
  • Processes cannot share ports (exception: TCP multicast).

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

18 of 39

Sockets

18

  • Both forms of communication, UDP and TCP, use the socket abstraction, which provides and endpoint for communication between processes.

  • Interprocess communication consists of transmitting a message between a socket in one process and a socket in another process.

(Figure 2)

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

19 of 39

Sockets

19

Figure 2. Sockets and ports

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

20 of 39

UDP Datagram Communication

20

  • UDP datagram properties
    • No guarantee of order preservation
    • Message loss and duplications are possible
  • Necessary steps
    • Creating a socket
    • Binding a socket to a port and local Internet address
      • A client binds to any free local port
      • A server binds to a server port

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

21 of 39

UDP Datagram Communication

21

  • Receive method
    • It returns Internet address and port of sender, plus message.

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

22 of 39

UDP Datagram Communication

22

  • Issues related to datagram communications are:
    • Message size
      • IP allows for messages of up to 216 bytes.
      • Most implementations restrict this to around 8 kbytes.
      • Any application requiring messages larger than the maximum must fragment.
      • If arriving message is too big for array allocated to receive message content, truncation occurs.

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

23 of 39

UDP Datagram Communication

23

    • Blocking
      • Send: non-blocking
        • upon arrival, message is placed in a queue for the socket that is bound to the destination port.
      • Receive: blocking
        • Pre-emption by timeout possible
        • If process wishes to continue while waiting for packet, use separate thread

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

24 of 39

UDP Datagram Communication

24

    • Timeout
    • Receive from any

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

25 of 39

UDP Datagram Communication

25

  • UDP datagrams suffer from following failures:
    • Omission failure
    • Messages may be dropped occasionally,
    • Ordering

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

26 of 39

Java API for UDP Datagrams

26

  • The Java API provides datagram communication by two classes:
    • DatagramPacket
      • It provides a constructor to make an array of bytes comprising:
        • Message content
        • Length of message
        • Internet address
        • Local port number
      • It provides another similar constructor for receiving a message.

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

27 of 39

Java API for UDP Datagrams

27

    • DatagramSocket
      • This class supports sockets for sending and receiving UDP datagram.
      • It provides a constructor with port number as argument.
      • No-argument constructor is used to choose a free local port.
      • DatagramSocket methods are:
        • send and receive
        • setSoTimeout
        • connect

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

28 of 39

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

29 of 39

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

30 of 39

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

31 of 39

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

32 of 39

TCP Stream Communication

32

  • The API to the TCP protocol provides the abstraction of a stream of bytes to be written to or read from.
    • Characteristics of the stream abstraction:
      • Message sizes
      • Lost messages
      • Flow control
      • Message destinations

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

33 of 39

TCP Stream Communication

33

  • Issues related to stream communication:
    • Matching of data items
    • Blocking
    • Threads

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

34 of 39

TCP Stream Communication

34

  • Use of TCP
    • Many services that run over TCP connections, with reserved port number are:
      • HTTP (Hypertext Transfer Protocol)
      • FTP (File Transfer Protocol)
      • Telnet
      • SMTP (Simple Mail Transfer Protocol)

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

35 of 39

TCP Stream Communication

35

  • Java API for TCP streams
    • The Java interface to TCP streams is provided in the classes:
      • ServerSocket
        • It is used by a server to create a socket at server port to listen for connect requests from clients.
      • Socket
        • It is used by a pair of processes with a connection.
        • The client uses a constructor to create a socket and connect it to the remote host and port of a server.
        • It provides methods for accessing input and output streams associated with a socket.

Couloris,Dollimore and Kindberg Distributed Systems: Concepts & Design Edn. 4 , Pearson Education 2005

INTERPROCESS COMMUNICATION

36 of 39

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

37 of 39

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

38 of 39

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

39 of 39

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