Networks and Client/Server Applications

Slides:



Advertisements
Similar presentations
Categories of I/O Devices
Advertisements

Chapter 15 Multithreading, Networks, and Client/Server Programming
Multiple Processor Systems
COEN 445 Communication Networks and Protocols Lab 4
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L23 (Chapter 25) Networking.
A Chat Server DBI – Representation and Management of Data on the Internet.
© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology 1 Let’s get started. Let’s start by selecting an architecture from among.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 25 Networking.
An Introduction to Internetworking. Algorithm for client-server communication with UDP (connectionless) A SERVER A CLIENT Create a server-socket (listener)and.
Networks and Client/Server Applications. Basics of Client/Server One host computer can have several servers Several clients can connect to a server Client.
Fundamentals of Python: From First Programs Through Data Structures
INTRODUCTION TO WEB DATABASE PROGRAMMING
Socket Programming References: redKlyde ’ s tutorial set Winsock2 for games (gamedev.net)
Programming Network Servers Topic 6, Chapters 21, 22 Network Programming Kansas State University at Salina.
Assignment 3 A Client/Server Application: Chatroom.
University of Pittsburgh Computer Science 1 Week 5: Introduction Last week we discussedLast week we discussed èDifference between executing sequentially.
1 Chapter Client-Server Interaction. 2 Functionality  Transport layer and layers below  Basic communication  Reliability  Application layer.
Threads. Sequential control Sequential programs: begin execute end At any time, there is a single point of execution Threads: also structured as begin.
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Unit – I CLIENT / SERVER ARCHITECTURE. Unit Structure  Evolution of Client/Server Architecture  Client/Server Model  Characteristics of Client/Server.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 11.
Copyright © Curt Hill, Client – Server Computing An important paradigm.
Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming.
1 Rick Mercer Multi Threaded Chat Server. 2 Client – Server with Socket Connections We've seen how to establish a connection with 1 client Review a simple.
Process by Dr. Amin Danial Asham. References Operating System Concepts ABRAHAM SILBERSCHATZ, PETER BAER GALVIN, and GREG GAGNE.
1 Chapter 28 Networking. 2 Objectives F To comprehend socket-based communication in Java (§28.2). F To understand client/server computing (§28.2). F To.
Operating Systems and Systems Programming CS162 Teaching Staff.
Networks and Client/Server Applications Handling Multiple Clients Concurrently.
Threaded Programming in Python Adapted from Fundamentals of Python: From First Programs Through Data Structures CPE 401 / 601 Computer Network Systems.
Distributed Computing A Programmer’s Perspective.
Client-Server Model of Interaction Chapter 20. We have looked at the details of TCP/IP Protocols Protocols Router architecture Router architecture Now.
CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network.
Processes CSCI 4534 Chapter 4. Introduction Early computer systems allowed one program to be executed at a time –The program had complete control of the.
1 Client-Server Interaction. 2 Functionality Transport layer and layers below –Basic communication –Reliability Application layer –Abstractions Files.
JS (Java Servlets). Internet evolution [1] The internet Internet started of as a static content dispersal and delivery mechanism, where files residing.
Module: Software Engineering of Web Applications Chapter 2: Technologies 1.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, written in Java code, that.
1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03
LECTURE 10 Networking. NETWORKING IN PYTHON Many Python applications include networking – the ability to communicate between multiple machines. We are.
LECTURE 11 Networking Part 2. NETWORKING IN PYTHON At this point, we know how to write a simple TCP client and simple TCP server using Python’s raw sockets.
A Local Area Network Chat Client ITTC LAN CHAT John Vincent Cecogo Jerikho Daguno Ardee Santos Elaine Mendoza Anjomar Pat Del Mindo Philip John Sales Philip.
R Some of these slides are from Prof Frank Lin SJSU. r Minor modifications are made. 1.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Threads by Dr. Amin Danial Asham. References Operating System Concepts ABRAHAM SILBERSCHATZ, PETER BAER GALVIN, and GREG GAGNE.
Chapter three.  An operating system executes a variety of programs:  A batch system executes jobs.  A time-shared systems has user programs or tasks.
CS 162 Discussion Section Week 2. Who am I? Prashanth Mohan Office Hours: 11-12pm Tu W at.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 33 Networking.
1 Chapter 1 INTRODUCTION TO WEB. 2 Objectives In this chapter, you will: Become familiar with the architecture of the World Wide Web Learn about communication.
Introduction to threads
Tiny http client and server
Assignment 3 A Client/Server Application: Chatroom
Threaded Programming in Python
Networks and Client/Server Applications
Lecture 10 Networking.
MCA – 405 Elective –I (A) Java Programming & Technology
Networks and Client/Server Applications
Chapter 4 Multithreading programming
Multiple Processor Systems
Multithreaded Programming
Threaded Programming in Python
Prof. Leonardo Mostarda University of Camerino
Computer Science 312 Concurrent Programming I Processes and Messages 1.
Computer Networks Protocols
Message Passing Systems Version 2
Message Passing Systems
Presentation transcript:

Networks and Client/Server Applications Handling Multiple Clients Concurrently

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

Chat Server and Client

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

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

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!

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

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

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

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

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: 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

For Wednesday Sorting