1 / 19

Networks and Client/Server Applications

Networks and Client/Server Applications. Handling Multiple Clients Concurrently. Problem. One server handles all clients Other clients will have to wait if one client has a big request. Server. Network. Client 1. Client 2. Client 3. Client N. A One-on-One Therapy Server. while True:

tara
Download Presentation

Networks and Client/Server Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Networks and Client/Server Applications Handling Multiple Clients Concurrently

  2. Problem • One server handles all clients • Other clients will have to wait if one client has a big request Server Network Client 1 Client 2 Client 3 Client N

  3. A One-on-One Therapy Server while True: print('Waiting for connection . . . ') client, address = server.accept() print('... connected from:', address) dr = Doctor() client.send(bytes(dr.greeting()), 'ascii')) while True: message = decode(client.recv(BUFSIZE), 'ascii’) if not message: print('Client disconnected') client.close() break else: client.send(bytes(dr.reply(message)), 'ascii')) Other patients must wait for the current patient to finish

  4. Multithreading • A modern computer can run several processes or threads concurrently • Each thread executes it own algorithm • Examples: • Edit text in a word processor while a spell checker runs in the background • Load an image file in the background while the Web browser lays out and displays a page

  5. States in the Life of a Thread new start The CPU ready The ready queue Schedules threads for the CPU

  6. States in the Life of a Thread new start yield or timed out running The CPU run ready The ready queue

  7. States in the Life of a Thread dead new complete start yield or timed out running The CPU run ready The ready queue

  8. The Thread Class Imported from the threading module Interface Thread() # Returns a new instance start() # Places the thread on the ready queue run() # Contains the code for the thread to execute # pass by default

  9. Using a Thread • Define a subclass of Thread with a new run method that contains the code to execute • Create an instance and run the start method to activate it

  10. A One-on-One Therapy Server while True: print('Waiting for connection . . . ') client, address = server.accept() print('... connected from:', address) dr = Doctor() client.send(bytes(dr.greeting()), 'ascii')) while True: message = decode(client.recv(BUFSIZE), 'ascii’) if not message: print('Client disconnected') client.close() break else: client.send(bytes(dr.reply(message)), 'ascii')) Other patients must wait for the current patient to finish

  11. A One-on-One Therapy Server while True: print('Waiting for connection . . . ') client, addr = server.accept() print('... connected from:', addr) dr = Doctor() handler = ClientHandler(client, dr) handler.start() Each client handler manages a conversation between a doctor and a client Because the client handlers run as separate threads, many of them can run concurrently The server can cycle back immediately to listen for another client

  12. The ClientHandler Class from threading import Thread class ClientHandler(Thread): The ClientHandler class is a subclass of Thread As such, objects of type ClientHandler can be used wherever threads are used

  13. The ClientHandler Class from threading import Thread class ClientHandler(Thread): def __init__(self, client, dr): Thread.__init__(self) self.client = client self.dr = dr The init method runs the init method in the Thread class receives the client socket and the doctor object from the server and transfers these to instance variables

  14. The ClientHandler Class from threading import Thread class ClientHandler(Thread): def __init__(self, client, dr): Thread.__init__(self) self.client = client self.dr = dr defrun(self): self.client.send(bytes(self.dr.greeting(), 'ascii')) while True: message = decode(self.client.recv(BUFSIZE), 'ascii') if not message: print('Client disconnected') client.close() break else: self.client.send(bytes(self.dr.reply(message), 'ascii'))

  15. A Multi-Client Chat Room • The server supports the chat but does not participate in it • Clients are identified by name (or pseudonym) • Many clients can send messages simultaneously • Each message is time-stamped and name-stamped • A reply from the server includes a complete record of the conversation thus far (time, user name, message)

  16. The Conversation Database • A database maintains a record of the conversation • The server creates and passes this common database to each client handler • A client handler adds clients’ messages to the database and retrieves the record to send to clients

  17. The Structure of the System Server Database Handler 1 Handler 2 Handler 3 Handler N Client 1 Client 2 Client 3 Client N

  18. The Client’s Task • The client prompts the user for her name • The client sends this name upon connecting to the server • The client receives a record of the conversation from the server • The client is prompted for a message • If the client simply presses Enter, close the connection and break • Otherwise, the message is sent to the server • Goto step 3

  19. The Client Handler’s Task • The handler receives the client’s name and saves it in an instance variable • The handler retrieves the record from the database and sends it to the client • The handler receives the client’s message • If the message is empty, close the connection and break • Otherwise, the handler name-stamps and time-stamps the message • The handler adds this message to the database • Goto step 2

More Related