
CS102: Interprocess Communication
Copyright © 1999,
Kenneth J. Goldman
Introduction
- So far in CS102, we've seen a variety of forms of communication in
software:
- communication among objects through method calls (params & return vals)
- communication with packages through APIs (with access control)
- communication among threads through shared objects (with synchronization)
- communication with human users through graphics components and events
- communication of error conditions through exception handling
- communication with the file system for saving and loading persistent state
using streams
- And last time we caught a glimpse of how the stream abstraction is general
enough to describe communication that occurs when a web browser or applet
downloads data from a web server across the network.
- The URL connection that we saw last time is a special case of a more general
form of communication:
- communication among processes, also known as interprocess communication
(IPC)
Interprocess Communication
- Interprocess communication differs from communication among threads within
the same program in several ways:
- Different processes execute in different address spaces. (They have separate
heaps.)
- Different processes may execute on different machines.
- Different processes may execute in different administration domains (and
may not trust each other.)
- These differences have certain implications for the way communication must
occur between different processes.
- Local memory adresses are meaningless on the "other side" if transmitted.
So, any data must be passed by value or if a reference is required, special
mechanisms must be in place to resolve the references (they can't be
ordinary addresses.)
- The network must be involved when processes live on different machines
- Care must be taken to ensure that sensitive data is not compromised by
untrusted applications involved in the communication (security) or by
applications snooping on the wire (encryption).
- Let's start by spending some time discussing briefly how communication occurs
in the network. Then we'll see how JAVA supports using this form of
communication. We'll only touch on the security issue in passing.
- If we take a simplified view of the internet, it might look something like
this:
- A collection of hosts (computers) at the edges of the network, where
- Each computer is connected to a local area network (LAN), and
- Every host has a unique address (called an IP address -- Internet
Protocol address)

Local Area Network (LAN)
- Within a Local Area Network (LAN), communication usually takes place
using the Ethernet protocol. Essentially:
- The Ethernet is a wire
- Only one computer can "talk" at a time on the wire or the bits get all
jumbled
- A computer can detect when more than one computer tries to talk at once and
so they both "back off" and try again
- All computers on the ethernet listen to all the bits on the wire and pick
up the messages that are addressed to them, and then press them along
to the relevant application(s).
- Each router is sort of like another host on the LAN, except that its
job is to forward messages (called IP packets) to other LANs or routers based
on the destination address. For example, a packet originally at sender(A)
would be picked up by router(1), forwarded to router(2), which would forward
it onto the LAN where receiver(B) is connected.
Internet Protocol (IP)
- The Internet Protocol (IP) is design to make its best effort to route packets
to their destination IP addresses. (Sort of like a electronic post office)
- However, since the wires are physical devices, sometimes there is interference,
or a link or router goes down, etc. Therefore, when a given IP packet is sent,
there is no guarantee that the packet will actually arrive at its destination
- To remediate this problem, another protocol layer (another layer of abstraction)
called the Transmission Control Protocol (TCP) sits on top of IP to
provide the illusion of reliable data transfer.
Transmission Control Protocol (TCP)
- TCP/IP works by a system of acknowledgements.
- A sender transmits packets, but also saves them on the side in a buffer
- When a receiver gets a packet, it sends an acknowledgement (ACK) to the
sender for that packet.
- If the sender doesn't get an ACK after a certain period of time, it
retransmits the packet.
- To improve throughput multiple packets (say n) are sent before
waiting for the first ACK and when the first ACK comes back, the next packet
(n+1) can be sent and so on. This is called a "sliding window protocol".
- Packets are delivered to the receiving application in the same order they
were sent by the sender. (FIFO)
- So, a TCP connection provides the illusion of reliable data transfer over IP,
which is inherently unreliable.
- A TCP connection is commonly referred to within applications as a socket,
which consists of the streams at each end, allowing communication in both
directions. In JAVA, the java.net API provides several classes you ccan use to
create and connect to sockets.
- Typical socket usage is not symmetric and involves two applications playing
two different roles:
- The Server -- this application creates a socket at a specified port number
on the local machine and then accepts connections on that socket
- The Client -- this application creates a socket that connects to an
existing socket already established by the server
- Note: Any application can play either role. In some cases, an application
may play both roles:

- Here, B is a client of A and a server for C.
Client Server Example
- In JAVA, the application acting as a server creates a ServerSocket object,
while the application acting as a client creates a Socket object. The server
calls the method accept() when it's ready to accept a connection from the
client. The accept method returns a Socket object that the server can use to
communicate with the client. Each socket provides the methods getInputStream()
and getOutputStream() which work as their names suggest.
- Here's an example of two applications that communicate through sockets. The
client sends an integer to the server, which squares the integer and sends
back the result. (Not a very useful application, but it illustrate the use
of sockets.)
- // Server code:
import java.net.*;
import java.io.*;
public class SquareServer {
- public static void main (String args[]) {
- try {
- ServerSocet ss = new ServerSocket (10450);
// 10450 = port number
Socket clientSocket = ss.accept();
DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream());
DataInputStream dis = new DataInputStream(clientSocket.getInputStream());
int result = Math.pow(dis.readInt(),2);
dos.writeInt(result);
clientSocket.close();
ss.close();
}
catch (Exception e) {
- System.out.println("Exception: " + e);
}
}
}
// Client code:
import java.net.*;
import java.io.*;
public class SquareClient {
- public static void main (String args[]) {
- try {
- Socket s = new Socket("hilton.cec.wustl.edu",10450);
// it automatically connects, assuming the server is
running on hilton
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
DataInputStream dis = new DataInputStream(s.getInputStream());
dos.writeInt(6);
int result = dis.readInt();
System.out.println("The square of 6 is " + result);
s.close();
}
catch (Exception e) {
- System.out.println("Exception " + e);
}
}
}
