Presentation is loading. Please wait.

Presentation is loading. Please wait.

Networks and Client/Server Applications

Similar presentations


Presentation on theme: "Networks and Client/Server Applications"— Presentation transcript:

1 Networks and Client/Server Applications
Handling Multiple Clients Concurrently

2 A One-on-One Chat Server
When a client connects, send a greeting and wait for a reply When the reply is received, send another message An empty string/reply should disconnect the client

3 Chat Server and Client

4 A One-on-One Chat Server
while True: print('Waiting for connection ') client, address = server.accept() print('... connected from:', address) client.send(bytes('Welcome to my chat room!', 'ascii')) message = decode(client.recv(BUFSIZE), 'ascii') if not message: print('Client disconnected') client.close() break else: print(message) client.send(bytes(input('> '), 'ascii')) Service includes a nested loop for carrying on the conversation

5 A One-on-One Chat Client
server = socket(AF_INET, SOCK_STREAM) server.connect(ADDRESS) print(decode(server.recv(BUFSIZE), 'ascii')) # Displays server’s # greeting while True: message = input('> ') if not message: break server.send(bytes(message, 'ascii')) reply = decode(server.recv(BUFSIZE), 'ascii') if not reply: print(reply) server.close() Client now has a loop to carry on the conversation Loop ends when the client sends or receives ''

6 Putting the Doctor Online
Very similar to a one-on-one chat, but the server responds by using a Doctor object’s reply instead of a human being’s input Minor changes to the chat server, but no changes at all to the chat client!

7 A One-on-One Chat Server
while True: print('Waiting for connection ') client, address = server.accept() print('... connected from:', address) client.send(bytes('Welcome to my chat room!', 'ascii')) message = decode(client.recv(BUFSIZE), 'ascii') if not message: print('Client disconnected') client.close() break else: print(message) client.send(bytes(input('> '), 'ascii')) Service includes a nested loop for carrying on the conversation

8 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')) message = decode(client.recv(BUFSIZE), 'ascii') if not message: print('Client disconnected') client.close() break else: client.send(bytes(dr.reply(message)), 'ascii')) Create the appropriate “bot” for carrying out the server’s side of the conversation

9 Going “Live”: the Server
from socket import * from time import ctime HOST = gethostbyname(gethostname()) PORT = 21566 ADDRESS = (HOST, PORT) server = socket(AF_INET, SOCK_STREAM) server.bind(ADDRESS) server.listen(5) while True: print('Waiting for connection ') client, address = server.accept() print('... connected from:', address) client.send(bytes(ctime() + '\nHave a nice day!', 'ascii')) client.close() Can deploy this server on any machine with an IP address

10 Going “Live”: the Client
from socket import * from codecs import decode HOST = input('Enter the server name: ') PORT = 21566 BUFSIZE = 1024 ADDRESS = (HOST, PORT) server = socket(AF_INET, SOCK_STREAM) server.connect(ADDRESS) dayAndTime = decode(server.recv(BUFSIZE), 'ascii') print(dayAndTime) server.close() The HOST must be the name or IP of the server

11 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

12 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')) 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

13 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

14 Multithreading Threads can share a single hardware processor by swapping (timesharing) Or they can run on separate processors in a multicore system

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

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

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

18 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

19 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

20 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')) 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

21 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 the 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

22 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

23 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

24 The ClientHandler Class
from threading import Thread class ClientHandler(Thread): def __init__(self, client, dr): Thread.__init__(self) self.client = client self.dr = dr def run(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'))

25 Extensions Create a separate Doctor object for each client
Save the client’s history list to a file If a file for a client exists, load the history list from it when she logs in Add separate GUIs for the client and the server

26 Extensibility The client/server pattern can be applied to many situations, such as the ATM and bank manager applications of this week’s lab Many ATM clients (on many different computers) One server/manager running on a single host

27 For Wednesday Sorting


Download ppt "Networks and Client/Server Applications"

Similar presentations


Ads by Google