Presentation is loading. Please wait.

Presentation is loading. Please wait.

School of Information Technologies Distributed Programming NETS3303/3603 Week 2.

Similar presentations


Presentation on theme: "School of Information Technologies Distributed Programming NETS3303/3603 Week 2."— Presentation transcript:

1 School of Information Technologies Distributed Programming NETS3303/3603 Week 2

2 School of Information Technologies 2 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

3 School of Information Technologies 3 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

4 School of Information Technologies 4 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

5 School of Information Technologies 5 Client - Server Process 1 Process 2 Process 3

6 School of Information Technologies 6 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

7 School of Information Technologies 7 P-2-P

8 School of Information Technologies 8 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

9 School of Information Technologies 9 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” A write B read pipe Flow of data © Qusay H. Mahmoud

10 School of Information Technologies 10 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

11 School of Information Technologies 11 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

12 School of Information Technologies 12 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

13 School of Information Technologies 13 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

14 School of Information Technologies 14 Addressing Protocol IP address 1 Ports Process 1 Process 2 Protocol IP address 2 Ports Process 1 Process 2

15 School of Information Technologies 15 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

16 School of Information Technologies 16 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

17 School of Information Technologies 17 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?

18 School of Information Technologies 18 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)

19 School of Information Technologies 19 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

20 School of Information Technologies 20 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); }

21 School of Information Technologies 21 Client/server Model A client initiates communication But, before this happens: Server has to listen for connection attempts

22 School of Information Technologies 22 TCP sockets Connection- oriented—connection remain open throughout duration, until ended Needs two programs for this application

23 School of Information Technologies 23 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); –2 nd argument causes output buffer to be flushed with every println

24 School of Information Technologies 24 TCP Server II Send and receive data –out.println(“Awaiting data…”); –message = in.readLine(); Close connection –link.close();

25 School of Information Technologies 25 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!"); }

26 School of Information Technologies 26 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

27 School of Information Technologies 27 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); }

28 School of Information Technologies 28 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

29 School of Information Technologies 29 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);

30 School of Information Technologies 30 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();

31 School of Information Technologies 31 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); }

32 School of Information Technologies 32 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

33 School of Information Technologies 33 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();

34 School of Information Technologies 34 Hint on objects How send complex structures? Objects with states? Arrays, vectors, matrices, hashtables? Just serialize! public class UserData implements java.io.Serializable

35 School of Information Technologies 35 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)

36 School of Information Technologies 36 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


Download ppt "School of Information Technologies Distributed Programming NETS3303/3603 Week 2."

Similar presentations


Ads by Google