ClientServer Paradigm and Performance L Grewe Review Basic
Client-Server Paradigm and Performance L. Grewe
Review: Basic Client-Server Request/Reply Paradigm Typical Internet app has two pieces: client and server Client (C): r initiates contact with server (“speaks first”) r typically requests service from a specific server r for Web, client is implemented in browser; for e-mail, in mail reader Server (S): r provides requested service to client r e. g. , Web server sends requested Web page; mail server delivers e-mail application transport network data link physical request reply application transport network data link physical 2
outgoing message queue Example: Electronic Mail user agent Three major components: • User agents • Mail servers • Protocols – Outgoing email • SMTP – Retrieving email user mailbox mail server SMTP • POP 3: Post Office Protocol [RFC 1939] • IMAP: Internet Mail Access Protocol [RFC 1730] mail server user agent 3 user agent mail server SMTP POP 3 or IMAP SMTP user agent
SMTP: Outgoing Email as a Client-Server Application S: C: S: C: C: C: S: 220 mr 1. its. yale. edu HELO cyndra. yale. edu 250 Hello cyndra. cs. yale. edu, pleased to meet you MAIL FROM: <spoof@cs. yale. edu> 250 spoof@cs. yale. edu. . . Sender ok RCPT TO: <yry@yale. edu> 250 yry@yale. edu. . . Recipient ok DATA 354 Enter mail, end with ". " on a line by itself Date: Wed, 23 Jan 2008 11: 20: 27 -0500 (EST) From: "Y. R. Yang" <yry@cs. yale. edu> To: "Y. R. Yang" <yry@cs. yale. edu> Subject: This is subject This is the message body! Please don’t spoof!. 250 Message accepted for delivery QUIT 221 mr 1. its. yale. edu closing connection 4
POP 3 Protocol: Mail Access Authorization phase S: C: S: • client commands: – user: declare username – pass: password • server responses – +OK – -ERR +OK POP 3 server ready user alice +OK pass hungry +OK user successfully logged C: list S: 1 498 S: 2 912 S: . C: retr 1 Transaction phase, client: S: <message 1 contents> • list: list message numbers S: . C: dele 1 • retr: retrieve message by C: retr 2 number S: <message 1 contents> • dele: delete S: . C: dele 2 • quit C: quit S: +OK POP 3 server signing off %openssl s_client –connect pop. gmail. com: 995 5 on
Big Picture buffers, states 6
Socket Programming with TCP Client must contact server • server process must first be running • server must have created welcome socket (door) that welcomes client’s contact Client contacts server by: • creating client-local TCP socket • specifying IP address, port number of server process • When client creates socket: client TCP establishes connection to server TCP • When contacted by client, server TCP creates new connection socket for server process to communicate with a specific client application viewpoint TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server
Big Picture: Connection-Oriented TCP 8
Connection-oriented TCP: Big Picture (C version) server client welcome. Socket=socket(): create socket client. Socket=socket(): create socket bind(welcome. Socket, …): specify socket address/port bind(client. Socket): specify socket address listen(welcome. Socket, …): specify that socket welcome. Socket is a listening socket connect(client. Socket, server. Addr, server. Port): initialize TCP handshake to server; return TCP connection setup until TCP handshake is done connection. Socket=accept(welcome. Socket, …): get a connected connection from the queue for socket welcome. Socket; create a new socket identified by connection. Socket read()/write(): do IO on client. Socket read()/write(): do IO on socket connection. Socket close(client. Socket): done close(connection. Socket): done 9 optional
Client/server socket interaction: TCP Server (running on hostid) Client create socket, port=x, for incoming request: welcome. Socket = Server. Socket(x) TCP wait for incoming connection setup connection request connection. Socket = welcome. Socket. accept() read request from connection. Socket write reply to connection. Socket close connection. Socket create socket, connect to servhost, port=x client. Socket = Socket() send request using client. Socket read reply from client. Socket close client. Socket
Server Flow Create Server. Socket(6789) conn. Socket = accept() read request from conn. Socket -Welcome socket: the waiting room -conn. Socket: the operation room Serve the request close conn. Socket
Server. Socket • Server. Socket() – creates an unbound server socket. • Server. Socket(int port) – creates a server socket, bound to the specified port. • Server. Socket(int port, int backlog) – creates a server socket and binds it to the specified local port number, with the specified backlog. • Server. Socket(int port, int backlog, Inet. Address bind. Addr) – create a server with the specified port, listen backlog, and local IP address to bind to. • bind(Socket. Address endpoint) – binds the Server. Socket to a specific address (IP address and port number). • bind(Socket. Address endpoint, int backlog) – binds the Server. Socket to a specific address (IP address and port number). • Socket accept() – listens for a connection to be made to this socket and accepts it. • close() closes this socket. 12
(Client)Socket • Socket(Inet. Address address, int port) creates a stream socket and connects it to the specified port number at the specified IP address. • Socket(Inet. Address address, int port, Inet. Address local. Addr, int local. Port) creates a socket and connects it to the specified remote address on the specified remote port. • Socket(String host, int port) creates a stream socket and connects it to the specified port number on the named host. • bind(Socket. Address bindpoint) binds the socket to a local address. • connect(Socket. Address endpoint) connects this socket to the server. • connect(Socket. Address endpoint, int timeout) connects this socket to the server with a specified timeout value. • Input. Stream get. Input. Stream() returns an input stream for this socket. • Output. Stream get. Output. Stream() returns an output stream for this socket. • close() closes this socket. 13
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 4) client reads, prints modified line from socket (in. From. Server stream) 14
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 Buffered. Reader in. From. User = input stream new Buffered. Reader(new Input. Stream. Reader(System. in)); sentence = in. From. User. read. Line(); Create client socket, Socket client. Socket = new Socket(“server. name", 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
Example: Java client (TCP), cont. Send line to server Create input stream attached to socket Read line from server out. To. Server. write. Bytes(sentence + 'n'); Buffered. Reader in. From. Server = new Buffered. Reader(new Input. Stream. Reader(client. Socket. get. Input. Stream())); modified. Sentence = in. From. Server. read. Line(); System. out. println("FROM SERVER: " + modified. Sentence); client. Socket. close(); } }
Example: Java server (TCP) 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()));
Example: Java server (TCP), cont Read in line from socket client. Sentence = in. From. Client. read. Line(); Create output capitalized. Sentence = client. Sentence. to. Upper. Case() + 'n'; stream, attached to socket Data. Output. Stream out. To. Client = new Data. Output. Stream(connection. Socket. get. Output. Stream()); Write out line out. To. Client. write. Bytes(capitalized. Sentence); to socket } } } End of while loop, loop back and wait for another client connection
Question • How inter-operable is the sample program? 19
Byte to Data: Char/String • Pay attention to encoding of data: transport layer handles only a sequence of bytes, the meaning of the bytes is by app. – String/char <-> bytes depends on charset • get. Bytes(“charset”) to get the correct char set • java. nio. charset. Charset. default. Charset() • Data. Output. Stream write. Bytes(String) truncates 20
Byte to Data: Data • Integers may be represented differently – Big vs little endianness Example: int x = 0 x 0 A 0 B 0 C 0 D What are the bytes? Typically network protocols are using big-endian order 21
Data Representation String/Char Int/short Byte 22
Clients and Servers • Get some sense of the dynamics of a basic client/server app
Server Model Welcome Socket (Queue) • Some basic questions – How long is the queue at the welcome socket? – What is the response time of a request? 24
Basic Modeling of C/S • Assume that client requests arrive at a rate of lambda/second • Assume that each request takes 1/mu seconds • Some basic questions – How long is the queue at the welcome socket? – What is the response time of a request? Welcome Socket Queue 25
Basic Modeling of C/S • We are not interested in extremely precise modeling, but want intuition • Assume that arrival and service patterns: memory less – During a small interval t, the probability of a new arrival is: t – During a small interval t, the probability of a current call finishes is: t 26
Analysis of Queue Length for C/S system state: # of requests queued at the welcome socket of the server 0 1 k p 0 p 1 pk k+1 N pk+1 p. N 27
Backup 28
UDP Client Server (running on hostid) create socket, port=x, for incoming request: server. Socket = Datagram. Socket( x ) create socket, client. Socket = Datagram. Socket() Create datagram using (servhost, x) as (dest addr. port), send request using client. Socket read request from server. Socket generate reply, create datagram using client host address, port number write reply to server. Socket read reply from client. Socket r Create socket with port number: close client. Socket Datagram. Socket s. Sock = new Datagram. Socket(9876); 29 r If no port number is specified, the OS will pick one
Example: UDPClient. java r A simple UDP client which reads input from keyboard, sends the input to server, and reads the reply back from the server. 30
Example: Java client (UDP) Create input stream 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)); String sentence = in. From. User. read. Line(); byte[] send. Data = new byte[1024]; send. Data = sentence. get. Bytes(); Create Datagram. Socket client. Socket = new Datagram. Socket(); client socket Translate Inet. Address s. IPAddress = Inet. Address. get. By. Name(“servname"); hostname to IP address using DNS 31
Example: Java client (UDP), cont. Create datagram with data-to-send, Datagram. Packet send. Packet = new Datagram. Packet(send. Data, send. Data. length, s. IPAddress, 9876); length, IP addr, port client. Socket. send(send. Packet); Send datagram to server byte[] receive. Data = new byte[1024]; 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(); } } 32
Example: UDPServer. java • A simple UDP server which changes any received sentence to upper case. 33
Example: Java server (UDP) Create datagram socket at port 9876 Create space for received datagram import java. io. *; import java. net. *; class UDPServer { public static void main(String args[]) throws Exception { Datagram. Socket server. Socket = new Datagram. Socket(9876); byte[] receive. Data = new byte[1024]; byte[] send. Data = new byte[1024]; while(true) { Datagram. Packet receive. Packet = new Datagram. Packet(receive. Data, receive. Data. length); Receive server. Socket. receive(receive. Packet); datagram 34
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 35
UDP Connectionless Demux Datagram. Socket server. Socket = new Datagram. Socket(9876); P 2 P 1 P 3 SP: 9876 DP: 9157 SP: 9876 DP: 5775 SP: 9157 client IP: A DP: 9876 server IP: S Source Port (SP) provides “return address” 36 SP: 5775 DP: 9876 Client IP: B
UDP Provides Multiplexing/Demultiplexing server UDP socket space 128. 36. 232. 5 128. 36. 230. 2 address: {*: 9876} snd/recv buf: local address client 198. 69. 10 UDP socket space address: {198. 69. 10: 1500} snd/recv buf: local port address: {128. 36. 232. 5: 53} snd/recv buf: address: {198. 69. 10: 4343} snd/recv buf: Packet demutiplexing is based on (dst address, dst port) at dst 37 %netstat –u –n -a
- Slides: 37