Concurrent Client server L Grewe Reveiw Client server socket interaction TCP Server running on host Welcome Socket Queue Client create socket port x for incoming request welcomeSocket ServerSocket x TCP wait for incoming connection setup connection request connectionSocket welcomeSocket accept read request from connectionSocket write reply to connectionSocket close connectionSocket create socket connect to serv host port x clientSocket Socket send request using clientSocket read reply from clientSocket close clientSocket Recap Data Representation Always pay attention to the data that you transfer the client and server may interpret the byte stream differently String Char Int short Byte 3 Recap State of Basic C S Welcome Socket Queue system state of requests queued at the welcome socket of the server 0 1 k k 1 p0 p1 pk pk 1 N pN Strategy if we know the fraction of time the server spends at each state we can get answers to some basic questions How long is the queue at the welcome socket What is the response time of a request 4 Events of Basic C S We are not interested in extremely precise modeling but want intuition System state changes upon events Let s focus on equilibrium Consider a simple arrival pattern client requests arrive at a rate of lambda second each request takes 1 mu seconds Assume memory less During a small interval t the number of new arrival is t During a small interval t the probability of a current request finishes is t 5 What is a Character of Equilibrium Time Reversibility state trend neither growing nor shrinking state k 1 k time 6 What Does Time Reversibility Imply Cannot distinguish f k k 1 f k 1 k bk k 1 bk 1 k state k 1 k time 7 Analysis of Queue Length for C S system state of requests queued at the welcome socke of the server 0 p0 1 p1 k pk k 1 pk 1 N pN at equilibrium time reversibility in one unit time transitions k k 1 transitions k 1 k pk pk 1 pk 1 pk k 1 p0 1 p0 2 1 N 1 8 Example Assume requests come in at a rate of one request per 20 seconds Each request takes on average 10 seconds What is the fraction of time that the welcome queue has a backlog of 3 requests 9 Server Flow Create ServerSocket 6789 connSocket accept read request from connSocket Processing request close connSocket Welcome Socket Queue Writing High Performance Servers Major Issues Many socket IO operations can cause a process to block e g accept waiting for new connection read a socket waiting for data or close write a socket waiting for buffer space I O read write for disk to finish Thus a crucial perspective of network server design is the concurrency design non blocking for high performance to avoid denial of service Concurrency is also important for clients Outline Recap Basic client server request reply Intro Basic socket programming Basic modeling Supporting concurrency 12 Multiplexing Demultiplexing Issue The server needs the capability to extract multiple requests from the welcome socket instead of one at a time Problem mutltiplexing since all clients to server use the same dest port Welcome Socket Queue 13 TCP Connection Oriented Demux TCP socket identified by 4 tuple source IP address source port number dest IP address dest port number recv host uses all four values to direct segment to appropriate socket server can easily support many simultaneous TCP sockets different connections sessions are automatically separated into different sockets Connection Oriented Demux P1 P4 P5 P2 P6 P1P3 SP x DP 25 S IP B D IP S client IP A SP x DP 25 S IP A D IP S server IP S SP source port number DP dest port number S IP source IP address D IP dest IP address SP y DP 25 S IP B D IP S Client IP B 15 Under the Hood TCP Multiplexing server TCP socket space 128 36 232 5 128 36 230 2 state listening address 6789 completed connection queue sendbuf recvbuf client 198 69 10 10 TCP socket space state starting address 198 69 10 10 1500 sendbuf recvbuf local port local addr remote port remote addr state listening address 25 completed connection queue sendbuf recvbuf netstat P tcp state listening address 25 completed connection queue sendbuf recvbuf puzzle netstat anv P tcp TCP IPv4 Local Remote Address Swind Snext Suna Rwind Rnext Rack Rto Mss State 0 00000000 00000000 49152 00000000 00000000 3375 1220 IDLE 134 154 14 51 22 66 123 67 238 61635 16304 00000030 00000000 49368 00000000 00000000 588 1452 ESTABLISHED more Example Client Initiates Connection server TCP socket space 128 36 232 5 128 36 230 2 state listening address 6789 completed connection queue sendbuf recvbuf state listening address 25 completed connection queue sendbuf recvbuf client 198 69 10 10 TCP socket space state connecting address 198 69 10 10 1500 128 36 232 5 6789 sendbuf recvbuf state listening address 25 completed connection queue sendbuf recvbuf Example TCP Handshake Done server TCP socket space 128 36 232 5 128 36 230 2 state listening address 6789 completed connection queue 128 36 232 5 6789 198 69 10 10 1500 sendbuf recvbuf state listening address 25 completed connection queue sendbuf recvbuf client 198 69 10 10 TCP socket space state connected address 198 69 10 10 1500 128 36 232 5 6789 sendbuf recvbuf state listening address 25 completed connection queue sendbuf recvbuf Example Server accept server TCP socket space 128 36 232 5 128 36 230 2 state listening address 6789 completed connection queue sendbuf recvbuf client 198 69 10 10 TCP socket space state connected address 198 69 10 10 1500 128 36 232 5 6789 sendbuf recvbuf state established address 128 36 232 5 6789 198 69 10 10 1500 sendbuf recvbuf state listening address 25 completed connection queue sendbuf recvbuf state listening address 25 completed connection queue sendbuf recvbuf Packet demutiplexing is based on dst addr dst port src addr src port Packet sent to the socket with the best match Outline Recap Basic client server request reply Intro Basic socket programming Basic modeling Supporting concurrency Multiplexing and demultiplexing Multi threads 21 Thread vs Process 22 Using Multi Threads for Servers A thread is a sequence of instructions which may execute in parallel with other threads We can have one thread for each client connection Thus only the flow thread processing a particular request is blocked Java Thread Model The Java virtual machine JVM creates the initial Java thread which executes the main method of the class passed to the JVM Most JVM s use POSIX threads to implement Java threads Threads in a Java program can be created Explicitly or
View Full Document
Unlocking...