Post on 21-Dec-2015
School of Information Technologies
Distributed Programming
NETS3303/3603
Week 2
2School of Information Technologies
Distributed Programming• What is a distributed system?
– Consists of a collection of computers linked together by a network
• Main characteristics:– Autonomous nodes with no global clock (asynchronous)– Communication is via message passing
• Advantages: enables computers to share resources (hardware, software, data)
• What is distributed programming?– To assign the work of a program to two or more processes—where
processes may exists on different computers
© Qusay H. Mahmoud
3School of Information Technologies
Architecture of Distributed Programming
• Goals:– Dividing responsibilities (processes) between
system components properly– Processes with well-defined responsibilities
interact with each other to perform a useful activity
© Qusay H. Mahmoud
4School of Information Technologies
Distributed Programming Models
• The client-server model– Most common
– Division of software in a client and a server process
– Server has access to some hw/sw resource that client wants to use
– A many-to-one relationship
– E.g.: web server, search engine
5School of Information Technologies
Client - Server
Process 1
Process 2Process 3
6School of Information Technologies
Distributed Programming Models
• Peer-to-Peer (aka multi-agent)– All nodes equal, both client and server (on the
rise)– All peers play similar roles– Can make requests as well as fulfil requests– Better interactive response– Examples: Skype, BitTorrent, Napster, Gnutella
7School of Information Technologies
P-2-P
8School of Information Technologies
Interprocess Communication (IPC)
• A set of programming APIs that coordinate activities among different processes to run concurrently in a network
• IPC methods include pipes, message queuing, semaphores, shared memory, and sockets
9School of Information Technologies
Process Communication• A pipe is a pseudo-file that can be used to connect two
processes together• Two related processes on a single machine may
communicate through a pipe
$ ls | grep “test”
Awrite
Bread
pipe
Flow of data
© Qusay H. Mahmoud
10School of Information Technologies
Client/Server Communication
• Two unrelated processes may communicate through files (process A write to a file and process B reads from it)
• How can two processes located on two different machines communicate?
• Solution: Berkeley Sockets (or BSD Sockets or Socket APIs)!
© Qusay H. Mahmoud
11School of Information Technologies
Socket
• A socket is an end point of a connection or an interface between user and network
• Used in the same way as a file descriptor:– 1- Creation (open socket)– 2- Receive/send (read/write from/to socket)– 3- Destruct (close socket)
© Qusay H. Mahmoud
12School of Information Technologies
Types of Sockets• A number of connections to choose from:
– TCP, UDP, Multicast
• Types of Sockets:– SOCK_STREAM (TCP sockets)– SOCK_DGRAM (UDP Sockets)– SOCK_RAW (ICMP)
• ICMP (Internet Control Message Protocol) sockets:– For security reasons not supported in Java (available in C)– Used in programs such as ping (echo request/reply)
© Qusay H. Mahmoud
13School of Information Technologies
Socket Addresses• Each socket has an address that specifies message
destinations• Each socket address is a communication identifier consists
of:– 1- Internet address– 2- Port number
• The port number is an integer that is needed to distinguish between services running on the same machine.– Port numbers between 0 .. 1023 are reserved by the system – user port numbers ≥ 1024
© Qusay H. Mahmoud
14School of Information Technologies
Addressing
Protocol Protocol
IP address 1
Ports
Process 1 Process 2
Protocol Protocol
IP address 2
Ports
Process 1 Process 2
15School of Information Technologies
Transport Protocols: Briefly• TCP: Transmission Control Protocol• UDP: User Datagram Protocol• TCP vs. UDP:
– In TCP two sockets must be connected before they can be used to transfer data whereas in UDP they must not
– In TCP, packets are acknowledged but in UDP, they are not.• Characteristics:
– TCP is a reliable protocol, UDP is not– TCP is connection-oriented, UDP is connectionless– TCP incurs overheads, UDP incurs fewer overheads– UDP has a size limit of 64k, in TCP no limit– UDP is easier to use and faster.
© Qusay H. Mahmoud
16School of Information Technologies
Communication Fundamentals
• For processes to exchange data:– Need pipe from process to stack– Need address to other process– Need knowledge of how data is represented and
agreement between processes
• Things happen both in:– User space– Kernel space
17School of Information Technologies
Data representation
• So now we know– Send data to IP x, Proto y, Port z
• But??– How do we interpret data?– How do we know what data to expect?– How do we know when things go right or
wrong?
18School of Information Technologies
Have Protocols!
• Protocols are agreements of– When we can send data– How data is represented; 8-bit, 16-bit etc.– How to interpret data / react
• For now– Transport protocols (UDP, TCP)– Application protocols (HTTP, SMTP,
GNUTELLA)
19School of Information Technologies
Java APIs J2SE 5.0: InetAddress
• In java.net package• Handles Internet address (name and IP)• Static method getByName
– returns host name as InetAddress– throws UnknownHostException (so catch it!)
• There are other useful methods like, isReachable(int timeout)– Check API javadocs for other methods
20School of Information Technologies
import java.net.*;import java.io.*;
public class IPFinder {
public static void main(String[] args) throws IOException {
String host;BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\n\nEnter host name: ");host = input.readLine();try {
InetAddress address = InetAddress.getByName(host);System.out.println(“Host: " + address.toString());
} catch (UnknownHostException e) {
System.out.println("Could not find " + host);}
}}
21School of Information Technologies
Client/server Model
• A client initiates communication
But, before this happens:
• Server has to listen for connection attempts
22School of Information Technologies
TCP sockets• Connection-oriented
—connection remain open throughout duration, until ended
• Needs two programs for this application
23School of Information Technologies
TCP Server• Create a ServerSocket object
– ServerSocket servSock = new ServerSocket (1234);– Server will listen for a connection from a client
• Put into waiting state (blocks)– Socket link = servSock.accept();
• Set up input & output streams– Use getInputStream and getOutputStream– BufferedReader input = new BufferedReader(
new InputStreamReader(link.getInputStream()));
– PrintWriter out = new
PrintWriter(link.getOutputStream(),true);– 2nd argument causes output buffer to be flushed with every println
24School of Information Technologies
TCP Server II
• Send and receive data– out.println(“Awaiting data…”);– message = in.readLine();
• Close connection– link.close();
25School of Information Technologies
public class TCPEchoServer{ private static ServerSocket servSock; private static final int PORT = 1234;
public static void main(String[] args) { System.out.println("Opening port...\n"); try { servSock = new ServerSocket(PORT); //Step 1. } catch(IOException e) { System.out.println("Unable to attach to port!"); System.exit(1); } do { run(); }while (true); }
private static void run() { Socket link = null; try { link = servSock.accept(); //Step 2.
BufferedReader in = new BufferedReader(new InputStreamReader(link.getInputStream()));
PrintWriter out = new PrintWriter( link.getOutputStream(),true); //Step 3.
int numMessages = 0; String message = in.readLine(); //Step 4. while (!message.equals("***CLOSE***")) { System.out.println("Message received."); numMessages++; out.println("Message " + numMessages + ": " + message); //Step 4. message = in.readLine(); } out.println(numMessages + " messages received.");
} catch(IOException e) { } finally {
try { System.out.println( "\n* Closing connection... *"); link.close(); //Step 5.}catch(IOException e) {
System.out.println("Unable to disconnect!");
} } }}
26School of Information Technologies
TCP Client
• Establish a connection to server– Socket link = new Socket(InetAddress.getLocalHost(),1234);
• Setup input/output streams
• Send and receive data
• Close the connection
27School of Information Technologies
public class TCPEchoClient{
private static InetAddress host;private static final int PORT = 1234;
public static void main(String[] args) {try { host = InetAddress.getLocalHost();}catch(UnknownHostException e) { System.out.println("Host ID not
found!"); System.exit(1);}run();
}
private static void run() { Socket link = null; //Step 1.try { link = new Socket(host,PORT); //Step 1.
BufferedReader in = new BufferedReader (new InputStreamReader(link.getInputStream())); PrintWriter out = new PrintWriter( link.getOutputStream(),true); //Step 2.
//Set up stream for keyboard entry...BufferedReader userEntry = new BufferedReader
(new InputStreamReader(System.in));
String message, response;do { System.out.print("Enter message: "); message = userEntry.readLine(); out.println(message); response = in.readLine(); //Step 3. System.out.println("\nSERVER> " + response);}while (!message.equals("***CLOSE***"));
} catch(IOException e) {}
finally {try {System.out.println("\n* Closing connection... *");link.close(); //Step 4.
} catch(IOException e) {
System.out.println("Unable to disconnect!");System.exit(1);
} } }}
28School of Information Technologies
UDP sockets
• Connectionless => no session• Each datagram is an isolated transmission• So it is faster!• And, server does not need to create a socket for
each client!• Both server and client use DatagramSocket,
and DatagramPacket are created and sent
29School of Information Technologies
UDP Server• Create a DatagramSocket object
– DatagramSocket dgramSock = new DatagramSocket(1234);
• Create a buffer for incoming datagram– Byte[] buff = new byte[256];
• Create a DatagramPacket for incoming datagram– DatagramPacket inPkt = new
DatagramPacket(buff,buff.length);• Accept an incoming datagram
– dgramSock.receive(inPkt);
30School of Information Technologies
UDP Server II• Get sender’s address and port
– InetAddress clientAdd = inPkt.getAddress();– int clientPort = inPkt.getPort();
• Retrieve data from buffer– String msg = new
String(inPkt.getData(),0,inPkt.getLength());• Create a response datagram
– DatagramPacket outPkt = new DatagramPacket (reply.getBytes(),reply.length(),clientAdd,clientPort);
– reply is a String object• Send response datagram
– dgramSock.send(outPkt);• Close socket
– dgramSock.close();
31School of Information Technologies
DatagramSocket server = new DatagramSocket(5000);
byte buffer[] = new byte[1000];
while (true) {
DatagramPacket request = new DatagramPacket(buffer,
buffer.length);
server.receive(request);
DatagramPacket reply = new DatagramPacket(request.
getData(), request.getLength(), request.getAddress(),
request.getPort());
server.send(reply);
}
32School of Information Technologies
UDP Client• Create a DatagramSocket object
– DatagramSocket dgramSock = new DatagramSocket();
• Create the outgoing datagram– DatagramPacket outPkt = new DatagramPacket (msg.getBytes(),msg.length(),host,PORT);
• Send datagram– dgramSock.send(outPkt);
• Receiving and processing daragram is same as server before
33School of Information Technologies
DatagramSocket client = new DatagramSocket();
String Msg=“Message”;
byte msg[] = Msg.getBytes();
int port = 5000;
InetAddress host = InetAddress.getByName(IPaddress);
DatagramPacket request = new DatagramPacket (msg,
Msg.length(), host, port);
client.send(request);
byte buffer[] = new byte[1000];
DatagramPacket reply= new DatagramPacket(buffer,buffer.length);
client.receive(reply);
System.out.println(“The reply is: “+reply.getData());
client.close();
34School of Information Technologies
Hint on objects
• How send complex structures?
• Objects with states?• Arrays, vectors,
matrices, hashtables?
Just serialize!
public class UserData implements java.io.Serializable
35School of Information Technologies
Loopback interface
• Use for local testing
• Run server and client on same host
• 127.0.0.1 or symbolic constant localhost
• Hint: Select high port numbers for testing (above 10000)
36School of Information Technologies
Summary
• Two programming models: C/S and p-2-p• Each has its own pros and cons• Sockets are used for any network
communications• Java provides both TCP and UDP sockets
APIs• Next: Multithreading and timers