Distributed Program Construction
Fall 2002
COMP 413
- Lack of shared memory requires message-based IPC.
- Protocols: standard rules that govern format, order, meaning, content of messages (e.g., retransmit message)
- The OSI Model: Layers of (independent) protocols
- Encapsulation on send - Each layer adds a header (sometimes trailer), and passes the results to level below
- Decapsulation on receive - strip layer's packet and pass to layer above
- Each layer is oblivious to packets in lower levels
Layered Protocols
COMP 413
- Physical - Transmit 0s and 1s. Signaling interface
- voltage, data transfer-rate, network connector
- Data Link - Error-free transmission (detection and correction)
- group bits into frames (leading and trailing separators), use checksum to detect errors (example: Ethernet)
- Network - purpose: route packets in a WAN. 2 types:
- Connection-oriented (ATM) - setup a route first, use for all subsequent traffic
- Connectionless (IP) - no setup, data divided into packets, each packet routed independent of all other ones.
- Transport - Reliable transmission of messages (e.g., TCP)
- Break messages into packets, assign sequence number
- Control over packets sent, recieved, capacity
- Over connectionless layers, messages may arrive in different order
- Session - dialog control, synchronization facilities (for checkpointing)
- Presentation - structuring of the data sent (e.g., employee record), independent of application. (e.g., XDR, encryption)
- Application - collection of protocols for common activities (e.g., telnet, ftp)
OSI Layers
COMP 413
IP - Network layer protocol (addressing)
Roles: 1. defines packet format and processing; 2. addressing hosts; 3. routing packets
IP global addressing scheme:
- IP address is a pair (netid, hostid) - network address assigned centrally, hostid assigned locally.
- Each IP address indicates a connection to a network (a host can have multiple IP addresses).
- 4 bytes: w.x.y.z w - determines the number of hosts in the network
- class A: 1-126 (up to 16M hosts)
- class B: 128-191 (up to 65536 hosts)
- class C: 192-223 (up to 256 hosts)
- class D: multicast networks
- all 1s in hostid - broadcast address
- 127 netid - loopback
- 0 netid - this network; all 0s, this host
- subnetting - allow multiple physical local networks with same netid
- IP consists of Internet portion (site) and local portion (physical local network+hostid)
COMP 413
- Minimal transport protocol
- differentiate among multiple sources via ports
- provides checksum to validate packets
- UDP message is encapsulated in an IP datagram
- No state management, control flow, data integrity
- no delivery guarantees. packets may be lost
- if receiver gets a corrupted packet, it is simply discarded
- any desired QoS should be programmed
- should be used when:
- transport overhead must be minimized (e.g., Video-on-demand)
- reliability is not crucial
- small, independent packets are sent (reduce overhead)
- examples: NFS, DNS, NTP, SNMP
COMP 413
TCP - Reliable Stream Transport
positive acknowledgement with retransmission:
- segments are ack. by receiver, If unack. after timeout, sender retransmits.
- problem - waste of bandwidth
- sliding window - window allows transmission of n octets before ack.
- when first octet gets ack., window slides by one
- if all octets in window sent, sender waits
- Variable window size - for end-to-end flow-control
- receiver sends with ack. a window advertisement
- sender varies window size accordingly
- Adaptive Timeout
- problem: impossible to know a priori how quickly ack. should return
- different networks, varying traffic
- solution: estimate timeout based on history
- RTT = (alpha * old_RTT) + ((1-alpha) * new_RTT_sample)
- timeout = beta * RTT (beta > 1)
- problem: what is a good value for beta ?
- when close to RTT - improves throughput (early detection), but wastes bandwidth (false detection)
- in early implementations beta = 2 (constant), recent methods are adaptive
COMP 413
TCP - Cont.
problem: endpoints see delay - might retransmit
solution: window = min(advertisement, congestion)
- segment loss -> reduce congestion win. by half
- remaining segments in window -> double timeout
- slow-start: increment size by 1 for each ack.
Connection identified by a pair of endpoints (host,port)
- an endpoint can be shared by multiple connections
- site 1 (active) sends SYN(x) (active open)
- site 2 (passive) replies with SYN(y) + ACK(x+1)
- site1 sends ACK(y+1)
- site 1 sends segments data (may be along ACK(y+1))
- site 2 ACKs, processes data, and replies
- site 1 closes (half) connection and sends FIN(x)
- site 2 sends ACK(x+1), closes connection, sends FIN(y) (avoids retransmission of Fin(x))
- site 1 sends ACK(y+1)
COMP 413
- Distributed mapping between hostnames and IP numbers
- the same host may have multiple names (aliases)
- the same name may refer to different hosts (RR DNS)
- Provides:
- lookup services (name resolver)
- maintains the database of hostnames
- arranged in a hierarchy of nameservers
- each host is configured to know about its local nameserver
- nameservers typically keep cache of hostnames to speedup lookup
COMP 413
- Stream: An abstraction of a connection to a communication channel (TCP/IP network, the memory, file, a terminal, etc.).
- Endpoint (port). Used to denote a communicating entity during network channel creation. I.e., in place of a file pathname, the combination of hostname, port is used to create a channel.
- In Java, data is written to the channel with an OutputStream and read
from the channel with an InputStream.
- FIFO: The first thing written to the OutputStream will be the first thing read from the corresponding InputStream.
- Sequential access: Allows to read/write bytes only one after the other. (There are several exceptions.)
- Read-only or write-only: A stream supports only writing to a channel or only reading from the channel.
- Blocking: A thread that reads/writes data blocks while no data is yet
available to read, or when the write operation is in progress. Very little
support for non-blocking I/O in Java.
COMP 413
The model:
- Class java.net.InetAddress represents a host address - its IP address (e.g., 128.42.1.197) and also, if available, its DNS name (e.g., vaud.cs.rice.edu).
- Class java.net.Socket represents a TCP connection for establishing a stream-based communication channel with a remote host.
- The remote address is designated by a host name (InetAddress) and a port number.
- The socket occupies a local port for communication.
- The getOutputStream and getInputStream methods return streams for access to the channel (to write to it and to read from it).
COMP 413
import java.io.*;
import java.net.*;
// Get an html page and print it on the console.
public class GetPage {
public static void main(String[] args) throws IOException {
// Get the URL.
URL url = new URL(args[0]);
String host = url.getHost();
int port = url.getPort();
String file = url.getFile();
if (port == -1) port = 80;
// Open a TCP socket.
Socket socket = new Socket(host, port);
// Prepare to read/write data.
OutputStream rawOut = socket.getOutputStream();
InputStream rawIn = socket.getInputStream();
BufferedOutputStream bufOut = new BufferedOutputStream(rawOut);
DataOutputStream out = new DataOutputStream(bufOut);
DataInputStream in = new DataInputStream(rawIn);
// Send the http request.
out.writeBytes("GET "+file+" HTTP/1.0\r\n\r\n");
out.flush();
// Receive the page and echo to the console.
String input;
while((input = in.readLine()) != null)
System.out.println(input);
}
}
COMP 413
- Class java.net.ServerSocket is the mechanism by which a server can accept connections from clients across a network.
- The procedure is this:
- A ServerSocket is opened on a particular local port, on the server host.
- Clients will connect to this port.
- For each connection ServerSocket creates a fresh Socket through which the server can communicate with the client.
- Notice:
- Available port numbers: 1 - 65535.
- Ports 1 - 1023 are reserved for system services and should not be used by applications.
- If port 0 is specified, then the operating system will select an arbitrary valid and free port.
- The operating system maintains a queue of client connections that were not yet accepted by the server. They are removed from the queue one-by-one as the server explicitly accepts connections.
COMP 413
import java.io.*;
import java.net.*;
// An echo server.
public class EchoServer {
public static void main(String[] args) throws IOException {
int port = Integer.parseInt(args[0]);
// Wait for client?s connection
ServerSocket server = new ServerSocket(port);
Socket client = server.accept();
server.close();
// Handle a connection and exit.
try {
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
new PrintStream(out).println("Welcome!");
int x;
while((x = in.read()) > -1)
out.write(x);
} finally {
client.close();
}
}
}
COMP 413
- There's no real support for non-blocking I/O in Java. The available() method of InputStream provides a workaround.
while(true) {
if (in1.available()) {
read from InputStream 1
process the data
}
if (in2.available()) {
read from InputStream 2
process the data
}
}
Multithreaded servers:
- Provides a better solution for dealing with multiple connections concurrently.
- A new thread is started per new connection.
ServerSocket server = new ServerSocket(port);
while(true) {
Socket client = server.accept();
// Creating and starting a separate new thread
// to handle the connection.
Thread thread = new ConnectionThread(client);
thread.start();
}
COMP 413
- Class java.net.DatagramPacket represents a single packet. It is used both for sending and receiving UDP packets.
- Sending a packet involves:
- Instantiating a DatagramPacket, while providing a message body, target address and port.
- Receiving a packet involves:
- Instantiating a DatagramPacket, while providing a buffer to which the accepted packet will be deposited.
- Class java.net.DatagramSocket represents a UDP socket. Used both for sending and receiving UDP packets.
- Sending and receiving packets is done with the send and receive methods respectively.
- UDP and TCP ports are different. Thus, a TCP socket can occupy (UDP) port n while a UDP socket occupies a (TCP) port n.
COMP 413
import java.net.*;
// An echo server, this time with UDP.
public class EchoServer {
public static void main(String[] args) throws IOException {
// Listen for incoming messages.
int port = Integer.parseInt(args[0]);
DatagramSocket socket = new DatagramSocket(port);
Byte[] buffer = new byte[65535];
DatagramPacket packet =
new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
// Handle a message.
InetAddress addr = packet.getAddress();
port = packet.getPort();
byte[] data = packet.getData();
int len = packet.getLength();
packet = new DatagramPacket(data,len,addr,port);
socket.send(packet);
}
}
import java.io.*;
import java.net.*;
// A client for the echo server.
public class EchoClient {
public static void main(String[] args) throws IOException {
InetAddress host = InetAddress.getByName(args[0]);
int port = Integer.parseInt(args[1]);
String message = args[2];
// Send the message.
ByteArrayOutputStream byteOut =
new ByteArrayOutputStream();
DataOutputStream dataOut =
new DataOutputStream(byteOut);
dataOut.writeUTF(message);
byte[] data = byteOut.toByteArray();
DatagramPacket packet =
new DatagramPacket(data,data.length,host,port);
DatagramSocket socket =
new DatagramSocket();
socket.send(packet);
// Receive and print the message.
byte[] buffer = new byte[65535];
packet = new DatagramPacket(buffer,buffer.length);
socket.receive(packet);
data = packet.getData();
int length = packet.getLength();
ByteArrayInputStream byteIn =
new ByteArrayInputStream(data, 0, length);
DataInputStream dataIn =
new DataInputStream(byteIn);
String result = dataIn.readUTF();
System.out.println("Received: "+result);
}
}
No comments:
Post a Comment