Скачать презентацию Process Communication COMPUTER NETWORKING Part 2 Client-server paradigm Скачать презентацию Process Communication COMPUTER NETWORKING Part 2 Client-server paradigm

a1d2444fdb27cc1ea4860b35a4599e49.ppt

  • Количество слайдов: 54

Process Communication COMPUTER NETWORKING Part 2 Client-server paradigm and Socket Programming ch 18 Thanks 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 korkmaz@cs. utsa. edu Distributed Systems 1. 1 TS

Computer Networking n Layered Protocols n Grand tour of computer networking, the Internet n Computer Networking n Layered Protocols n Grand tour of computer networking, the Internet n Client-server paradigm, n Socket Programming Distributed Systems 1. 2 TS

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

The client/server model underlies almost every distributed system (networked applications). Hence it is important The client/server model underlies almost every distributed system (networked applications). Hence it is important to understand the principles of client/server communication. CLIENT-SERVER COMMUNICATION MODELS Distributed Systems 1. 4 TS

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

OPT Request Protocol (R) n If service l does not have output parameters and OPT Request Protocol (R) n If service l does not have output parameters and l does not have a return type client may not want to wait for server to finish. request send(. . . ) Client Distributed Systems receive(. . . ) exec op; Continue execution 1. 6 Server TS

Request-Reply protocol (RR) n To be applied if client expects result from server l Request-Reply protocol (RR) n To be applied if client expects result from server l Client requests service execution from server through request message, and l Delivery of service result in reply message n Most client-server interactions are built on RR protocol request send(. . . ) receive(. . . ) Client reply blocked receive(. . . ) exec op; send(. . . ) Server message exchange protocol : format, order, action Distributed Systems 1. 7 TS

OPT Request-Reply-Acknowledge Protocol (RRA) n In addition to RR protocol, client sends acknowledgement after OPT Request-Reply-Acknowledge Protocol (RRA) n In addition to RR protocol, client sends acknowledgement after it received reply n Acknowledgement sent asynchronously request send(. . . ) receive(. . . ) send (. . . ) reply history Client Distributed Systems 1. 8 receive(. . . ) exec op; send(. . . ) receive(. . . ) Server TS

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

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

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

OPT Buffering Issues n Unbuffered communication l Server must call receive before client can OPT Buffering Issues n Unbuffered communication l Server must call receive before client can call send n Buffered communication l user server Client send to a mailbox l user Server receives from a mailbox Distributed Systems 1. 12 TS

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

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

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

Learn how to build client/server applications that communicate with each other using sockets SOCKET Learn how to build client/server applications that communicate with each other using sockets SOCKET PROGRAMMING Distributed Systems 1. 19 From Computer Networking by Kurose and Ross. TS

What is a socket? socket Socket API a host-local, application-created, OS-controlled interface (a “door”) What is a socket? 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 n introduced in BSD 4. 1 UNIX, 1981 n explicitly created, used, released by apps n client/server paradigm n two types of transport service via socket API: l UDP: unreliable datagram l TCP: reliable, in-order byte-stream Distributed Systems 1. 20 From Computer Networking by Kurose and Ross. TS

SOCKET PROGRAMMING C Distributed Systems 1. 21 From Computer Networking by Kurose and Ross. SOCKET PROGRAMMING C Distributed Systems 1. 21 From Computer Networking by Kurose and Ross. TS

Socket-programming using TCP service: connection-oriented, reliable, in-order byte-stream controlled by application developer controlled by Socket-programming using TCP service: connection-oriented, reliable, in-order byte-stream controlled by application developer controlled by operating system process socket TCP with buffers, variables internet controlled by operating system server client Distributed Systems socket TCP with buffers, variables controlled by application developer 1. 22 From Computer Networking by Kurose and Ross. TS

Socket programming with TCP n Client must know n Server process must first be Socket programming with TCP n Client must know n Server process must first be running server’s IP and port n Server creates a TCP welcome n Client then l creates a TCP socket l connects it to server by specifying IP address and port number of server process socket and binds it to a known port and waits for requests l read/write data n When contacted by client, server creates new TCP socket for server process to communicate with client n 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 Distributed Systems 1. 23 From Computer Networking by Kurose and Ross. TS

***** Client/server socket interaction: TCP Server (running on hostid, port x) // create a ***** Client/server socket interaction: TCP Server (running on hostid, port x) // create a TCP socket, // bind to port x, welcome. Socket = socket(…, SOCK_STREAM, 0); bind(welcome. Socket, x); listen(welcome. Socket); TCP wait for incoming connection request connection. Socket = accept(welcome. Socket) (running on hostname ? , port ? ) // create socket, // connect to hostid, port x client. Socket = socket( …, SOCK_STREAM, 0) setup connect(client. Socket, hostid, x) write request using client. Socket read request from connection. Socket write reply to connection. Socket read reply from client. Socket close connection. Socket Distributed Systems Client close client. Socket 1. 24 TS

Connection-oriented demux: Threaded Web Server can fork a child process to handle each request Connection-oriented demux: Threaded Web Server can fork a child process to handle each request from different clients Too costly, use threads! P 1 P 2 P 4 connect P 1 P 3 SP: 5775 SP: 9157 DP: 80 S-IP: B S-IP: A D-IP: C SP: 9157 client IP: A DP: 80 S-IP: A server IP: C S-IP: B Client IP: B D-IP: C Distributed Systems DP: 80 1. 25 From Computer Networking by Kurose and Ross. TS

***** TCP Socket Primitives Connection socket Primitive Function Socket Create a new communication endpoint ***** TCP Socket Primitives 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 some data over the connection Recv Receive some data over the connection Close Release the connection Distributed Systems 1. 26 TS

OPT USP ch 18: Socket Implementation of UICI Distributed Systems 1. 27 TS OPT USP ch 18: Socket Implementation of UICI Distributed Systems 1. 27 TS

OPT USP ch 18: UICI: Client/Server Interaction Distributed Systems 1. 28 TS OPT USP ch 18: UICI: Client/Server Interaction Distributed Systems 1. 28 TS

Example: C server (TCP) Complete example is posted at the class web site. See Example: C server (TCP) 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 welcome. Socket, connection. Socket; /* socket descriptor */ struct hostent *ptrh; /* pointer to a host table entry */ Create welcoming socket at port & Bind a local address char client. Sentence[128]; char capitalized. Sentence[128]; port = atoi(argv[1]); welcome. Socket = 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(welcome. Socket, (struct sockaddr *)&sad, sizeof(sad)); Distributed Systems 1. 29 TS

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

Example: C client (TCP) /* client. c */ void main(int argc, char *argv[]) { Example: C client (TCP) /* client. c */ void main(int argc, char *argv[]) { struct sockaddr_in sad; /* structure to hold an IP address */ int client. Socket; /* socket descriptor */ struct hostent *ptrh; /* pointer to a host table entry */ Create client socket, connect to server char Sentence[128]; char modified. Sentence[128]; host = argv[1]; port = atoi(argv[2]); client. Socket = 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_length); connect(client. Socket, (struct sockaddr *)&sad, sizeof(sad)); Distributed Systems 1. 31 TS

Example: C client (TCP), cont Get input stream from user Send line to server Example: C client (TCP), cont Get input stream from user Send line to server Read line from server Close connection Distributed Systems gets(Sentence); n=write(client. Socket, Sentence, strlen(Sentence)+1); n=read(client. Socket, modified. Sentence, sizeof(modified. Sentence)); printf("FROM SERVER: %sn”, modified. Sentence); close(client. Socket); } 1. 32 TS

Other related functions n getpeername( ) n gethostbyaddr( ) n getsockopt( ) if ( Other related functions n getpeername( ) n gethostbyaddr( ) n getsockopt( ) if ( (pid=fork()) == 0) { /* CHILD PROC */ close(welcome. Socket); /* give service */ exit(0); } /* PARENT PROC */ close(connection. Socket); n setsockopt ( ) Too costly, use threads! n sigaction(SIGINT, …); Distributed Systems 1. 33 TS

OPT Waiting something from both socket and stdin FD_ZERO(&rset); FD_SET(welcome. Socket, &rset); FD_SET(fileno(stdin), &rset); OPT Waiting something from both socket and stdin FD_ZERO(&rset); FD_SET(welcome. Socket, &rset); FD_SET(fileno(stdin), &rset); maxfd =max(welcome. Socket, fileno(stdin)) + 1; select(maxfd, &rset, NULL, NULL); if (FD_ISSET(fileno(stdin), &rset)){ /* read something from stdin */ } Distributed Systems 1. 34 TS

Exercise: implement a client-server program using TCP sockets. n The client will first establish Exercise: implement a client-server program using TCP sockets. n 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. n 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) n Make sure you close the socket descriptors that are not needed in the parent and child. n 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. TS Distributed Systems 1. 35

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

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

OPT Connectionless demux (cont) Server create UDP Socket and binds it to port 6428 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) P 2 P 1 P 3 SP: 6428 DP: 9157 DP: 5775 SP: 9157 client IP: A DP: 6428 SP: 5775 server IP: C DP: 6428 Client IP: B SP provides “return address” Distributed Systems 1. 38 From Computer Networking by Kurose and Ross. TS

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

OPT Example: C server (UDP) Complete example is posted at the class /* server. OPT Example: C server (UDP) Complete example is posted at the class /* server. c */ web site. See the socket programming link void main(int argc, char *argv[]) under “Online Materials” { struct sockaddr_in sad; /* structure to hold an IP address */ struct sockaddr_in cad; int server. Socket; /* socket descriptor */ struct hostent *ptrh; /* pointer to a host table entry */ Create welcoming socket at port & Bind a local address char client. Sentence[128]; char capitalized. Sentence[128]; port = atoi(argv[1]); server. Socket = 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(server. Socket, (struct sockaddr *)&sad, sizeof(sad)); Distributed Systems 1. 40 TS

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

OPT Example: C client (UDP) /* client. c */ void main(int argc, char *argv[]) OPT Example: C client (UDP) /* client. c */ void main(int argc, char *argv[]) { struct sockaddr_in sad; /* structure to hold an IP address */ int client. Socket; /* socket descriptor */ struct hostent *ptrh; /* pointer to a host table entry */ char Sentence[128]; char modified. Sentence[128]; Create client socket, NO connection to server host = argv[1]; port = atoi(argv[2]); client. Socket = 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_length); Distributed Systems 1. 42 TS

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

EXTRAS for self study SOCKET PROGRAMMING JAVA Distributed Systems 1. 44 From Computer Networking EXTRAS for self study SOCKET PROGRAMMING JAVA Distributed Systems 1. 44 From Computer Networking by Kurose and Ross. TS

Client/server socket interaction: TCP Server (running on hostid, port x) Client (running on hostname Client/server socket interaction: TCP Server (running on hostid, port x) Client (running on hostname ? , port ? ) create socket, port=x, for incoming request: welcome. Socket = Server. Socket() TCP wait for incoming connection request connection. Socket = welcome. Socket. accept() setup send request using client. Socket read request from connection. Socket write reply to connection. Socket read reply from client. Socket close connection. Socket Distributed Systems create socket, connect to hostid, port=x client. Socket = Socket() close client. Socket 1. 45 From Computer Networking by Kurose and Ross. TS

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

Example: Java server (TCP) Complete example is posted at the class web site. See 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 { Create welcoming socket at port 6789 Wait, on welcoming socket for contact by client Create input stream, attached to socket public static void main(String argv[]) throws Exception { String client. Sentence; String capitalized. Sentence; Server. Socket welcome. Socket = new Server. Socket(6789); while(true) { Socket connection. Socket = welcome. Socket. accept(); Buffered. Reader in. From. Client = new Buffered. Reader(new Input. Stream. Reader(connection. Socket. get. Input. Stream())); Distributed Systems 1. 47 From Computer Networking by Kurose and Ross. TS

Example: Java server (TCP), cont Create output stream, attached Data. Output. Stream out. To. Example: Java server (TCP), cont Create output stream, attached Data. Output. Stream out. To. Client = to socket new Data. Output. Stream(connection. Socket. get. Output. Stream()); Read in line client. Sentence = in. From. Client. read. Line(); from socket capitalized. Sentence = client. Sentence. to. Upper. Case() + 'n'; Write out line out. To. Client. write. Bytes(capitalized. Sentence); to socket } } End of while loop, } loop back and wait for another client connection Distributed Systems 1. 48 From Computer Networking by Kurose and Ross. TS

Example: Java client (TCP) import java. io. *; import java. net. *; class TCPClient Example: Java client (TCP) import java. io. *; import java. net. *; class TCPClient { public static void main(String argv[]) throws Exception { String sentence; String modified. Sentence; Create input stream Buffered. Reader in. From. User = new Buffered. Reader(new Input. Stream. Reader(System. in)); Create client socket, Socket client. Socket = new Socket(“localhost", 6789); connect to server Create Data. Output. Stream out. To. Server = output stream new Data. Output. Stream(client. Socket. get. Output. Stream()); attached to socket Distributed Systems 1. 49 From Computer Networking by Kurose and Ross. TS

Example: Java client (TCP), cont. Create Buffered. Reader in. From. Server = input stream Example: Java client (TCP), cont. Create Buffered. Reader in. From. Server = input stream new Buffered. Reader(new attached to socket Input. Stream. Reader(client. Socket. get. Input. Stream())); sentence = in. From. User. read. Line(); Send line to server out. To. Server. write. Bytes(sentence + 'n'); Read line modified. Sentence = in. From. Server. read. Line(); from server System. out. println("FROM SERVER: " + modified. Sentence); client. Socket. close(); } } Distributed Systems 1. 50 From Computer Networking by Kurose and Ross. TS

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

Example: Java client (UDP) Client process Input: receives packet (recall that. TCP received “byte Example: Java client (UDP) Client process Input: receives packet (recall that. TCP received “byte stream”) Output: sends packet (recall that TCP sent “byte stream”) client UDP socket Distributed Systems 1. 52 From Computer Networking by Kurose and Ross. TS

Example: Java server (UDP) Complete example is posted at the class web site. See 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 Create { datagram socket at port 9876 Datagram. Socket server. Socket = new Datagram. Socket(9876); byte[] receive. Data = new byte[1024]; byte[] send. Data = new byte[1024]; while(true) { Create space for received datagram Datagram. Packet receive. Packet = new Datagram. Packet(receive. Data, receive. Data. length); Receive server. Socket. receive(receive. Packet); datagra m Distributed Systems 1. 53 From Computer Networking by Kurose and Ross. TS

Example: Java server (UDP), cont String sentence = new String(receive. Packet. get. Data()); Get Example: Java server (UDP), cont String sentence = new String(receive. Packet. get. Data()); Get IP addr Inet. Address IPAddress = receive. Packet. get. Address(); port #, of sender int port = receive. Packet. get. Port(); String capitalized. Sentence = sentence. to. Upper. Case(); send. Data = capitalized. Sentence. get. Bytes(); Create datagram Datagram. Packet send. Packet = to send to client new Datagram. Packet(send. Data, send. Data. length, IPAddress, port); Write out datagram server. Socket. send(send. Packet); to socket } } } End of while loop, loop back and wait for another datagram Distributed Systems 1. 54 From Computer Networking by Kurose and Ross. TS

Example: Java client (UDP) Create input stream Create client socket Translate hostname to IP Example: Java client (UDP) Create input stream Create client socket Translate hostname to IP address using DNS Distributed Systems import java. io. *; import java. net. *; class UDPClient { public static void main(String args[]) throws Exception { Buffered. Reader in. From. User = new Buffered. Reader(new Input. Stream. Reader(System. in)); Datagram. Socket client. Socket = new Datagram. Socket(); Inet. Address IPAddress = Inet. Address. get. By. Name(“localhost"); byte[] send. Data = new byte[1024]; byte[] receive. Data = new byte[1024]; String sentence = in. From. User. read. Line(); send. Data = sentence. get. Bytes(); 1. 55 From Computer Networking by Kurose and Ross. TS

Example: Java client (UDP), cont. Create datagram with data-to-send, length, IP addr, Datagram. Packet Example: Java client (UDP), cont. Create datagram with data-to-send, length, IP addr, Datagram. Packet send. Packet = new Datagram. Packet(send. Data, send. Data. length, IPAddress, 9876); port Send datagram client. Socket. send(send. Packet); to server Datagram. Packet receive. Packet = new Datagram. Packet(receive. Data, receive. Data. length); Read datagram from server client. Socket. receive(receive. Packet); String modified. Sentence = new String(receive. Packet. get. Data()); System. out. println("FROM SERVER: " + modified. Sentence); client. Socket. close(); } } Distributed Systems 1. 56 From Computer Networking by Kurose and Ross. TS

import java. io. *; import java. net. *; class TCPServer { public static void import java. io. *; import java. net. *; class TCPServer { public static void main(String argv[]) throws Exception { String client. Sentence; String capitalized. Sentence; Multi threaded Server. Socket welcome. Socket = new Server. Socket(6789); while(true) { Socket connection. Socket = welcome. Socket. accept(); Buffered. Reader in. From. Client = new Buffered. Reader(new Input. Stream. Reader(connection. Socket. get. Input. Stream())); Data. Output. Stream out. To. Client = new Data. Output. Stream(connection. Socket. get. Output. Stream()); client. Sentence = in. From. Client. read. Line(); capitalized. Sentence = client. Sentence. to. Upper. Case() + 'n'; out. To. Client. write. Bytes(capitalized. Sentence); } } Distributed Systems 1. 57 TS