Presentation is loading. Please wait.

Presentation is loading. Please wait.

Process Communication COMPUTER NETWORKING Part 2

Similar presentations


Presentation on theme: "Process Communication COMPUTER NETWORKING Part 2"— Presentation transcript:

1 Process Communication COMPUTER NETWORKING Part 2
Client-server paradigm and Socket Programming ch 18 Thanks to the authors of the textbook [USP] and [KR] for providing the base slides. I made several changes/additions. These slides may incorporate materials kindly provided by Prof. Dakai Zhu. So I would like to thank him, too. Turgay Korkmaz

2 Computer Networking Layered Protocols
Grand tour of computer networking, the Internet Client-server paradigm, Socket Programming

3 Objectives To understand how processes communicate (the heart of distributed systems) To understand computer networks and their layers (part 1) To understand client-server paradigm and Use low-level message passing using sockets

4 Client-Server Communication models
The client/server model underlies almost every distributed system (networked applications). Hence it is important to understand the principles of client/server communication. In this second part, we are going to use the knowledge about communication primitives that we just acquired to explain the principles of client/server communication. In client/server communication many things can go wrong and a good client/server communication protocol takes this into account and delivers reliable results even in the presence of failures. Messages are occasionally dropped. Network may become partitioned. Processes may fail. No corruption of data, as ensured by error-checking mechanisms at the network level. We are going to discuss different degrees of quality of service as a basis for the discussion of three client server protocols. The simplest of these protocols is the request protocol, which only achieves a limited quality and cannot generally be applied. The most important protocol is the request/reply protocol. It delivers either the result of a service execution or a failure to the client. An optimization is the request/reply/acknowledgement protocol. Client-Server Communication models

5 Client-server architecture
communicate with server may have dynamic IP addresses and port do not communicate directly with each other may be intermittently connected to the network servers: always-on host permanent IP address, well-known port server farms for scaling Clients and servers communicate through Socket, RPC, RMI From Computer Networking by Kurose and Ross.

6 Request Protocol (R) If service
OPT Request Protocol (R) If service does not have output parameters and does not have a return type client may not want to wait for server to finish. request send(...) Client Server receive(...) exec op; Continue execution The R protocol may be used when there is no value to be returned from the procedure and the client requires no confirmation that the procedure has been executed. The client may proceed immediately after the request message is sent as there is no need to wait for a reply message. Perfect system: Sites and network never fail, no message loss, no corrupted messages The R protocol is ok TCP/IP: No corrupted message; message loss, site and communication failures may occur The R protocol only offers best-effort Client does not detect any communication or site failure Server fails before execution service Communication fails before delivering message Message loss Example: Notification kind message/service; RIP (Routing Information protocol)

7 Request-Reply protocol (RR)
To be applied if client expects result from server Client requests service execution from server through request message, and Delivery of service result in reply message Most client-server interactions are built on RR protocol send(...) receive(...) Client Server exec op; request reply blocked The general message exchange protocol for client-server applications The RR protocol is useful for most client-server exchanges because it is based on the request-reply protocol. Special acknowledgement messages are not required because a server’s reply message is regarded as an acknowledgement of the client’s request message. Similarly, a subsequent call from a client may be regarded as an acknowledgement of a server’s reply message. message exchange protocol : format, order, action

8 Request-Reply-Acknowledge Protocol (RRA)
OPT Request-Reply-Acknowledge Protocol (RRA) In addition to RR protocol, client sends acknowledgement after it received reply Acknowledgement sent asynchronously send(...) receive(...) send (...) Client Server exec op; request reply history The RRA protocol is based on the exchange of three messages: request-reply-acknowledge reply. The acknowledge reply message contains the requestId from the reply message being acknowledged. The arrival of a requestId in an acknowledgement message will be interpreted as acknowledging the receipt of all reply messages with lower requestIds, so the loss of an acknowledgement message is harmless. Although the exchange involves an additional message, it need not block the client as acknowledgement may be transmitted after the reply has been given to the client, but it does use processing and network resources.

9 Issues in Client-Server Communication
Addressing (IP address, port number) Blocking versus non-blocking Buffered versus unbuffered Reliable versus unreliable Server architecture: concurrent versus sequential Scalability

10 Addressing Issues OPT Question: how does the client know where the server is located? Hard-wired address Machine address and process address are known a priori ( port 80) DNS converts www… to 32-bit IP Broadcast-based Server chooses address from a sparse address space Client broadcasts request Can cache response for future Locate address via name server user server user server NS user server

11 Blocking versus Non-blocking
OPT Blocking versus Non-blocking Blocking communication (synchronous) Sender blocks until message is actually sent Receiver blocks until message is actually received Non-blocking communication (asynchronous) Sender returns immediately Receiver does not block either Examples:

12 Buffering Issues Unbuffered communication Buffered communication
OPT Buffering Issues Unbuffered communication Server must call receive before client can call send Buffered communication Client send to a mailbox Server receives from a mailbox user server user server

13 Reliability Unreliable channel Reliable channel
OPT Reliability Unreliable channel Need acknowledgements (ACKs) Applications handle ACKs ACKs for both request and reply Reliable channel Reply acts as ACK for request Explicit ACK for response Reliable communication on unreliable channels Transport protocol (TCP) handles lost messages request ACK User reply Server ACK request User reply Server ACK

14 Server Architecture Sequential Concurrent Thus servers could be
Serve one request at a time Can service multiple requests by employing events and asynchronous communication Concurrent Server spawns a process (or thread) to service each request Can also use a pre-spawned pool of threads/processes (apache) Thus servers could be Pure-sequential, event-based, thread-based, process-based Discussion: which architecture is most efficient?

15 Scalability Question: How can you scale the server capacity?
OPT Scalability Question: How can you scale the server capacity? Buy bigger machine! Replicate Distribute data and/or algorithms Ship code instead of data Cache

16 Learn how to build client/server applications that communicate with each other using sockets
Socket programming From Computer Networking by Kurose and Ross.

17 What is a socket? Socket API a host-local, application-created,
OS-controlled interface (a “door”) that hides the details of all the layers and provides transport services to application process so it can send and receive messages to/from another application process socket Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm two types of transport service via socket API: UDP: unreliable datagram TCP: reliable, in-order byte-stream From Computer Networking by Kurose and Ross.

18 Socket programming C From Computer Networking by Kurose and Ross.

19 Socket-programming using TCP
TCP service: connection-oriented, reliable, in-order byte-stream controlled by application developer controlled by application developer process TCP with buffers, variables socket process TCP with buffers, variables socket (i.e., transfer bytes from one process to another, no structure!) controlled by operating system controlled by operating system internet server client From Computer Networking by Kurose and Ross.

20 Socket programming with TCP
Client must know server’s IP and port Client then creates a TCP socket connects it to server by specifying IP address and port number of server process read/write data Server process must first be running Server creates a TCP welcome socket and binds it to a known port and waits for requests When contacted by client, server creates new TCP socket for server process to communicate with client read/write data Server can talk with multiple clients TCP socket is identified by 4-tuple: source IP address source port number dest IP address dest port number From Computer Networking by Kurose and Ross.

21 Client/server socket interaction: TCP
***** Client/server socket interaction: TCP Server (running on hostid, port x) Client (running on hostname ?, port ?) // create a TCP socket, // bind to port x, welcomeSocket = socket(…,SOCK_STREAM,0); bind(welcomeSocket, x); listen(welcomeSocket); // create socket, // connect to hostid, port x clientSocket = socket( …, SOCK_STREAM, 0) connect(clientSocket, hostid, x) TCP connection setup wait for incoming connection request connectionSocket = accept(welcomeSocket) write request using clientSocket read request from connectionSocket write reply to read reply from clientSocket close close connectionSocket

22 Connection-oriented demux: Threaded Web Server
Server can fork a child process to handle each request from different clients Too costly, use threads! P1 P4 P2 P1 P3 SP: 9157 DP: 80 S-IP: A D-IP:C connect SP: 5775 DP: 80 D-IP:C S-IP: B SP: 9157 DP: 80 D-IP:C S-IP: B SP: 9157 DP: 80 S-IP: A D-IP:C client IP: A Client IP:B server IP: C From Computer Networking by Kurose and Ross.

23 TCP Socket Primitives ***** Primitive Function Socket
Connection socket Primitive Function Socket Create a new communication endpoint Bind Attach a local address to a socket Listen Announce willingness to accept connections Accept Block caller until a connection request arrives Connect Actively attempt to establish a connection Send Send some data over the connection Recv Receive some data over the connection Close Release the connection

24 USP ch 18: Socket Implementation of UICI
OPT USP ch 18: Socket Implementation of UICI

25 USP ch 18: UICI: Client/Server Interaction
OPT USP ch 18: UICI: Client/Server Interaction

26 Create welcoming socket at port
Complete example is posted at the class web site. See the socket programming link under “Online Materials” Example: C server (TCP) /* server.c */ void main(int argc, char *argv[]) { struct sockaddr_in sad; /* structure to hold an IP address */ struct sockaddr_in cad; int welcomeSocket, connectionSocket; /* socket descriptor */ struct hostent *ptrh; /* pointer to a host table entry */ char clientSentence[128]; char capitalizedSentence[128]; port = atoi(argv[1]); welcomeSocket = socket(PF_INET, SOCK_STREAM, 0); memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */ sad.sin_family = AF_INET; /* set family to Internet */ sad.sin_addr.s_addr = INADDR_ANY; /* set the local IP address */ sad.sin_port = htons((u_short)port);/* set the port number */ bind(welcomeSocket, (struct sockaddr *)&sad, sizeof(sad)); Create welcoming socket at port & Bind a local address

27 Example: C server (TCP), cont
/* Specify the maximum number of clients that can be queued */ listen(welcomeSocket, 10) while(1) { connectionSocket=accept(welcomeSocket, (struct sockaddr *)&cad, &alen); n=read(connectionSocket, clientSentence, sizeof(clientSentence)); /* capitalize Sentence and store the result in capitalizedSentence*/ n=write(connectionSocket, capitalizedSentence, strlen(capitalizedSentence)+1); close(connectionSocket); } Wait, on welcoming socket for contact by a client Write out the result to socket End of while loop, loop back and wait for another client connection

28 Example: C client (TCP)
/* client.c */ void main(int argc, char *argv[]) { struct sockaddr_in sad; /* structure to hold an IP address */ int clientSocket; /* socket descriptor */ struct hostent *ptrh; /* pointer to a host table entry */ char Sentence[128]; char modifiedSentence[128]; host = argv[1]; port = atoi(argv[2]); clientSocket = socket(PF_INET, SOCK_STREAM, 0); memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */ sad.sin_family = AF_INET; /* set family to Internet */ sad.sin_port = htons((u_short)port); ptrh = gethostbyname(host); /* Convert host name to IP address */ memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length); connect(clientSocket, (struct sockaddr *)&sad, sizeof(sad)); Create client socket, connect to server

29 Example: C client (TCP), cont
Get input stream from user gets(Sentence); n=write(clientSocket, Sentence, strlen(Sentence)+1); n=read(clientSocket, modifiedSentence, sizeof(modifiedSentence)); printf("FROM SERVER: %s\n”,modifiedSentence); close(clientSocket); } Send line to server Read line from server Close connection

30 Other related functions
if ( (pid=fork()) == 0) { /* CHILD PROC */ close(welcomeSocket); /* give service */ exit(0); } /* PARENT PROC */ close(connectionSocket); getpeername( ) gethostbyname( ) gethostbyaddr( ) getsockopt( ) setsockopt ( ) sigaction(SIGINT,…); Too costly, use threads!

31 Waiting something from both socket and stdin
OPT Waiting something from both socket and stdin FD_ZERO(&rset); FD_SET(welcomeSocket, &rset); FD_SET(fileno(stdin), &rset); maxfd =max(welcomeSocket,fileno(stdin)) + 1; select(maxfd, &rset, NULL, NULL, NULL); if (FD_ISSET(fileno(stdin), &rset)){ /* read something from stdin */ }

32 Exercise: implement a client-server program using TCP sockets.
The client will first establish a TCP connection with the server. It then sends the name of a customer and expects server to send back the balance of that customer. Upon receiving the balance, the client will print it and quit. The server always waits for connection requests in an infinite loop. Upon receiving a connection request, the server (parent process) creates a child process, which performs the task(s) expected by the client. The parent process (server) will continue to wait for another client request. Suppose Server keeps accounts in an array of struct account { char name[20]; double balance;} (create a static array of 10 customers with random names, balance in your program) Make sure you close the socket descriptors that are not needed in the parent and child.  When implementing client and server programs, please focus on the key system calls and in which order to call them while using generic parameters (e.g., IPaddress, portnum) rather than actual structures. Also ignore error checking for clarity and assume all the necessary libraries are included.

33 Socket programming with UDP
OPT Socket programming with UDP UDP: no “connection” between client and server no handshaking sender explicitly attaches IP address and port of destination to each packet server must extract IP address, port of sender from received packet UDP: transmitted data may be received out of order, or lost application viewpoint UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server From Computer Networking by Kurose and Ross.

34 Client/server socket interaction: UDP
OPT Client/server socket interaction: UDP Server (running on hostid, port x) create a UDP socket, clientSocket = socket(…, SOCK_DGRAM, 0); Client (running on hostname ?, port ?) Create, address (hostid, port=x) send datagram request using clientSocket Create a UDP socket, port=x, for incoming request: serverSocket = socket(…, SOCK_DGRAM, 0); bind(serverSocket, x); receive request from serverSocket close clientSocket recv reply from clientSocket send reply to serverSocket specifying client host address, port number

35 Connectionless demux (cont)
OPT Connectionless demux (cont) Server create UDP Socket and binds it to port 6428 Clients create a UDP socket (port number will be dynamically assigned) P2 client IP: A P1 P1 P3 SP: 6428 DP: 9157 SP: 6428 DP: 5775 SP: 9157 DP: 6428 SP: 5775 DP: 6428 Client IP:B server IP: C SP provides “return address” From Computer Networking by Kurose and Ross.

36 UDP: Connectionless demultiplexing
OPT UDP: Connectionless demultiplexing When host receives UDP segment: checks destination port number in segment directs UDP segment to socket with that port number IP datagrams with different source IP addresses and/or source port numbers directed to same socket UDP socket is identified by two-tuple: (dest IP address, dest port number) From Computer Networking by Kurose and Ross.

37 Create welcoming socket at port
OPT Example: C server (UDP) Complete example is posted at the class web site. See the socket programming link under “Online Materials” /* server.c */ void main(int argc, char *argv[]) { struct sockaddr_in sad; /* structure to hold an IP address */ struct sockaddr_in cad; int serverSocket; /* socket descriptor */ struct hostent *ptrh; /* pointer to a host table entry */ char clientSentence[128]; char capitalizedSentence[128]; port = atoi(argv[1]); serverSocket = socket(PF_INET, SOCK_DGRAM, 0); memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */ sad.sin_family = AF_INET; /* set family to Internet */ sad.sin_addr.s_addr = INADDR_ANY; /* set the local IP address */ sad.sin_port = htons((u_short)port);/* set the port number */ bind(serverSocket, (struct sockaddr *)&sad, sizeof(sad)); Create welcoming socket at port & Bind a local address

38 Example: C server (UDP), cont
OPT Example: C server (UDP), cont while(1) { n=recvfrom(serverSocket, clientSentence, sizeof(clientSentence), 0 (struct sockaddr *) &cad, &addr_len ); /* capitalize Sentence and store the result in capitalizedSentence*/ n=sendto(serverSocket, capitalizedSentence, strlen(capitalizedSentence)+1,0 (struct sockaddr *) &cad, &addr_len); } close(serverSocket); Receive messages from clients End of while loop, loop back and wait for another client connection Write out the result to socket

39 NO connection to server
OPT Example: C client (UDP) /* client.c */ void main(int argc, char *argv[]) { struct sockaddr_in sad; /* structure to hold an IP address */ int clientSocket; /* socket descriptor */ struct hostent *ptrh; /* pointer to a host table entry */ char Sentence[128]; char modifiedSentence[128]; host = argv[1]; port = atoi(argv[2]); clientSocket = socket(PF_INET, SOCK_DGRAM, 0); /* determine the server's address */ memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */ sad.sin_family = AF_INET; /* set family to Internet */ sad.sin_port = htons((u_short)port); ptrh = gethostbyname(host); /* Convert host name to IP address */ memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length); Create client socket, NO connection to server

40 Example: C client (UDP), cont.
OPT Example: C client (UDP), cont. Get input stream from user gets(Sentence); addr_len =sizeof(struct sockaddr); n=sendto(clientSocket, Sentence, strlen(Sentence)+1, (struct sockaddr *) &sad, addr_len); n=recvfrom(clientSocket, modifiedSentence, sizeof(modifiedSentence). (struct sockaddr *) &sad, &addr_len); printf("FROM SERVER: %s\n”,modifiedSentence); close(clientSocket); } Send line to server Read line from server Close connection

41 Socket programming JAVA
EXTRAS for self study Socket programming JAVA From Computer Networking by Kurose and Ross.

42 Client/server socket interaction: TCP
Server (running on hostid, port x) Client (running on hostname ?, port ?) create socket, port=x, for incoming request: welcomeSocket = ServerSocket() TCP connection setup close connectionSocket read reply from clientSocket create socket, connect to hostid, port=x clientSocket = Socket() wait for incoming connection request connectionSocket = welcomeSocket.accept() send request using clientSocket read request from connectionSocket write reply to From Computer Networking by Kurose and Ross.

43 Socket programming with TCP
Example client-server app: 1) client reads line from standard input (inFromUser stream) , sends to server via socket (outToServer stream) 2) server reads line from socket 3) server converts line to uppercase, sends back to client 4) client reads, prints modified line from socket (inFromServer stream) Client process Stream jargon A stream is a sequence of characters that flow into or out of a process. An input stream is attached to some input source for the process, e.g., keyboard or socket. An output stream is attached to an output source, e.g., monitor or socket. client TCP socket From Computer Networking by Kurose and Ross.

44 Example: Java server (TCP)
Complete example is posted at the class web site. See the socket programming link under “Online Materials” import java.io.*; import java.net.*; class TCPServer { public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence; ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); Create welcoming socket at port 6789 Wait, on welcoming socket for contact by client Create input stream, attached to socket From Computer Networking by Kurose and Ross.

45 Example: Java server (TCP), cont
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n'; outToClient.writeBytes(capitalizedSentence); } Create output stream, attached to socket Read in line from socket Write out line to socket End of while loop, loop back and wait for another client connection From Computer Networking by Kurose and Ross.

46 Example: Java client (TCP)
import java.io.*; import java.net.*; class TCPClient { public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket(“localhost", 6789); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); Create input stream Create client socket, connect to server Create output stream attached to socket From Computer Networking by Kurose and Ross.

47 Example: Java client (TCP), cont.
Create input stream attached to socket BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.readLine(); outToServer.writeBytes(sentence + '\n'); modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence); clientSocket.close(); } Send line to server Read line from server From Computer Networking by Kurose and Ross.

48 Client/server socket interaction: UDP
Server (running on hostid) create socket, clientSocket = DatagramSocket() Client (running on hostname ?, port ?) Create datagram with server IP and port=x; send datagram via clientSocket create socket, port= x. serverSocket = DatagramSocket() read datagram from serverSocket close clientSocket read datagram from write reply to serverSocket specifying client address, port number From Computer Networking by Kurose and Ross.

49 Example: Java client (UDP)
process Input: receives packet (recall thatTCP received “byte stream”) Output: sends packet (recall that TCP sent “byte stream”) client UDP socket From Computer Networking by Kurose and Ross.

50 Example: Java server (UDP)
Complete example is posted at the class web site. See the socket programming link under “Online Materials” import java.io.*; import java.net.*; class UDPServer { public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(true) DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); Create datagram socket at port 9876 Create space for received datagram Receive datagram From Computer Networking by Kurose and Ross.

51 Example: Java server (UDP), cont
String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase(); sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); } Get IP addr port #, of sender Create datagram to send to client Write out datagram to socket End of while loop, loop back and wait for another datagram From Computer Networking by Kurose and Ross.

52 Example: Java client (UDP)
import java.io.*; import java.net.*; class UDPClient { public static void main(String args[]) throws Exception { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName(“localhost"); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; String sentence = inFromUser.readLine(); sendData = sentence.getBytes(); Create input stream Create client socket Translate hostname to IP address using DNS From Computer Networking by Kurose and Ross.

53 Example: Java client (UDP), cont.
Create datagram with data-to-send, length, IP addr, port DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); } Send datagram to server Read datagram from server From Computer Networking by Kurose and Ross.

54 Multi threaded import java.io.*; import java.net.*; class TCPServer {
public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence; ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n'; outToClient.writeBytes(capitalizedSentence); } Multi threaded


Download ppt "Process Communication COMPUTER NETWORKING Part 2"

Similar presentations


Ads by Google