4
Interprocess Communication
Figure 4.1
Middleware layers
Figure 4.2
Sockets and ports
any port agreed port
socket socket
message
client server
other ports
Internet address = [Link] Internet address = [Link]
Figure 4.3
UDP client sends a message to the server and gets a reply
import [Link].*;
import [Link].*;
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 = args[0].getBytes();
InetAddress aHost = [Link](args[1]);
int serverPort = 6789;
DatagramPacket request = new DatagramPacket(m, [Link](), aHost, serverPort);
[Link](request);
byte[] buffer = new byte[1000];
DatagramPacket reply = new DatagramPacket(buffer, [Link]);
[Link](reply);
[Link]("Reply: " + new String([Link]()));
}catch (SocketException e){[Link]("Socket: " + [Link]());
}catch (IOException e){[Link]("IO: " + [Link]());}
}finally {if(aSocket != null) [Link]();}
}
}
Figure 4.4
UDP server repeatedly receives a request and sends it back to the client
import [Link].*;
import [Link].*;
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, [Link]);
[Link](request);
DatagramPacket reply = new DatagramPacket([Link](),
[Link](), [Link](), [Link]());
[Link](reply);
}
}catch (SocketException e){[Link]("Socket: " + [Link]());
}catch (IOException e) {[Link]("IO: " + [Link]());}
}finally {if(aSocket != null) [Link]();}
}
}
Figure 4.5
TCP client makes connection to server, sends request and receives reply
import [Link].*;
import [Link].*;
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(args[1], serverPort);
DataInputStream in = new DataInputStream( [Link]());
DataOutputStream out =
new DataOutputStream( [Link]());
[Link](args[0]); // UTF is a string encoding see Sn 4.3
String data = [Link]();
[Link]("Received: "+ data) ;
}catch (UnknownHostException e){
[Link]("Sock:"+[Link]());
}catch (EOFException e){[Link]("EOF:"+[Link]());
}catch (IOException e){[Link]("IO:"+[Link]());}
}finally {if(s!=null) try {[Link]();}catch (IOException e){[Link]("close:"+[Link]());}}
}
}
Figure 4.6
TCP server makes a connection for each client and then echoes the client’s request
import [Link].*;
import [Link].*;
public class TCPServer {
public static void main (String args[]) {
try{
int serverPort = 7896;
ServerSocket listenSocket = new ServerSocket(serverPort);
while(true) {
Socket clientSocket = [Link]();
Connection c = new Connection(clientSocket);
}
} catch(IOException e) {[Link]("Listen :"+[Link]());}
}
}
// this figure continues on the next slide
Figure 4.6 continued
class Connection extends Thread {
DataInputStream in;
DataOutputStream out;
Socket clientSocket;
public Connection (Socket aClientSocket) {
try {
clientSocket = aClientSocket;
in = new DataInputStream( [Link]());
out =new DataOutputStream( [Link]());
[Link]();
} catch(IOException e) {[Link]("Connection:"+[Link]());}
}
public void run(){
try { // an echo server
String data = [Link]();
[Link](data);
} catch(EOFException e) {[Link]("EOF:"+[Link]());
} catch(IOException e) {[Link]("IO:"+[Link]());}
} finally{ try {[Link]();}catch (IOException e){/*close failed*/}}
}
}
RPC sample server and client
See server.c and client.c
9
Figure 4.7
CORBA CDR for constructed types
Type Representation
sequence length (unsigned long) followed by elements in order
string length (unsigned long) followed by characters in order (can also
can have wide characters)
array array elements in order (no length specified because it is fixed)
struct in the order of declaration of the components
enumerated unsigned long (the values are specified by the order declared)
union type tag followed by the selected member
Figure 4.8
CORBA CDR message
index in notes
sequence of bytes 4 bytes on representation
0–3 5 length of string
4–7 "Smit" ‘Smith’
8–11 "h___"
12–15 6 length of string
16–19 "Lond" ‘London’
20-23 "on__"
24–27 1984 unsigned long
The flattened form represents a Person struct with value: {‘Smith’, ‘London’, 1984}
Figure 4.9
Indication of Java serialized form
Serialized values Explanation
Person 8-byte version number h0 class name, version number
int year [Link] [Link] number, type and name of
3 name: place: instance variables
1984 5 Smith 6 London h1 values of instance variables
The true serialized form contains additional type markers; h0 and h1 are handles
Figure 4.10 XML definition of the Person structure
<person id="123456789">
<name>Smith</name>
<place>London</place>
<year>1984</year>
<!-- a comment -->
</person >
Figure 4.11 Illustration of the use of a namespace in the Person structure
<person pers:id="123456789" xmlns:pers = "[Link]
<pers:name> Smith </pers:name>
<pers:place> London </pers:place >
<pers:year> 1984 </pers:year>
</person>
Figure 4.12 An XML schema for the Person structure
<xsd:schema xmlns:xsd = URL of XML schema definitions >
<xsd:element name= "person" type ="personType" />
<xsd:complexType name="personType">
<xsd:sequence>
<xsd:element name = "name" type="xs:string"
<xsd:element name = "place" type="xs:string"
<xsd:element name = "year" type="xs:positive
</xsd:sequence>
<xsd:attribute name= "id" type = "xs:positiveInteger
</xsd:complexType>
</xsd:schema>
Figure 4.13
Representation of a remote object reference
32 bits 32 bits 32 bits 32 bits
interface of
Internet address port number time object number
remote object
Figure 4.14
Multicast peer joins a group and sends and receives datagrams
import [Link].*;
import [Link].*;
public class MulticastPeer{
public static void main(String args[]){
// args give message contents & destination multicast group (e.g. "[Link]")
MulticastSocket s =null;
try {
InetAddress group = [Link](args[1]);
s = new MulticastSocket(6789);
[Link](group);
byte [] m = args[0].getBytes();
DatagramPacket messageOut =
new DatagramPacket(m, [Link], group, 6789);
[Link](messageOut);
// this figure continued on the next slide
Figure 4.14
continued
// get messages from others in group
byte[] buffer = new byte[1000];
for(int i=0; i< 3; i++) {
DatagramPacket messageIn =
new DatagramPacket(buffer, [Link]);
[Link](messageIn);
[Link]("Received:" + new String([Link]()));
}
[Link](group);
}catch (SocketException e){[Link]("Socket: " + [Link]());
}catch (IOException e){[Link]("IO: " + [Link]());}
}finally {if(s != null) [Link]();}
}
}
Figure 4.15
Types of overlay
table continues on the next slide
19
Figure 4.15 (continued)
Types of overlay
20
Figure 4.16
Skype overlay architecture
21
Figure 4.17
An overview of point-to-point communication in MPI
22
Figure 4.18
Selected send operations in MPI
23