CS102: Interprocess Communication

Copyright © 1999, Kenneth J. Goldman


So far in CS102, we've seen a variety of forms of communication in software:

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:

Interprocess Communication

Interprocess communication differs from communication among threads within the same program in several ways:

  1. Different processes execute in different address spaces. (They have separate heaps.)
  2. Different processes may execute on different machines.
  3. 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.

  1. 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.)
  2. The network must be involved when processes live on different machines
  3. 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:

Local Area Network (LAN)

Within a Local Area Network (LAN), communication usually takes place using the Ethernet protocol. Essentially:

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.

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);
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());
int result = dis.readInt();
System.out.println("The square of 6 is " + result);
catch (Exception e) {
System.out.println("Exception " + e);