Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores.

Slides:



Advertisements
Similar presentations
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores.
Advertisements

Multiplexing/Demux. CPSC Transport Layer 3-2 Multiplexing/demultiplexing application transport network link physical P1 application transport network.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores.
Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones.
Object-Oriented Software Engineering Concurrent Programming.
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
1 Java Networking – Part I CS , Spring 2008/9.
Programming Applets How do Applets work ? This is an HTML page This is the applet’s code It has a link to an applet.
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores.
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Server Architecture Models Operating Systems Hebrew University Spring 2004.
What happens when many clients want to contact the server at the same time ?  The iterative approach consist in “enqueuing” the requests (this is done.
An Introduction to Internetworking. Why distributed systems - Share resources (devices & CPU) - Communicate people (by transmitting data)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L22 (Chapter 25) Networking.
© 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.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
An Introduction to Internetworking. Algorithm for client-server communication with UDP (connectionless) A SERVER A CLIENT Create a server-socket (listener)and.
Implementing a concurrent file server (as an extension of Thread) Define a new Class as an extension of the Thread class which has a socket as Object variable.
What happens when many clients want to contact the server at the same time ? The iterative approach consist in “enqueuing” the requests (this is done automatically.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
19-Aug-15 About the Chat program. 2 Constraints You can't have two programs (or two copies of the same program) listen to the same port on the same machine.
Programming Network Servers Topic 6, Chapters 21, 22 Network Programming Kansas State University at Salina.
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
1 Chapter Client-Server Interaction. 2 Functionality  Transport layer and layers below  Basic communication  Reliability  Application layer.
An program As a simple example of socket programming we can implement a program that sends to a remote site As a simple example of socket.
Jozef Goetz, Application Layer PART VI Jozef Goetz, Position of application layer The application layer enables the user, whether human.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Threads and Processes.
1 CSC111H Client-Server: An Introduction Dennis Burford
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Operating Systems Recitation 9, May 19-20, Iterative server Handle one connection request at a time. Connection requests stored in queue associated.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
1 Web Based Programming Section 8 James King 12 August 2003.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
© Lethbridge/Laganière 2005 Chap. 3: Basing Development on Reusable Technology The Client-Server Architecture A distributed system is a system in.
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.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
CE Operating Systems Lecture 13 Linux/Unix interprocess communication.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Chapter 6 (6.7 & 6.9 only) - Threads & Mutex Threads –A Thread is a basic unit of CPU utilization consisting of a program counter, register set and stack.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
1 CSCD 330 Network Programming Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 9 Client-Server Programming.
Part 4: Network Applications Client-server interaction, example applications.
Multi-Threading in Java
Lecture 4 Mechanisms & Kernel for NOSs. Mechanisms for Network Operating Systems  Network operating systems provide three basic mechanisms that support.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp Advanced Java Programming CSE 7345/5345/ NTU 531 Multithreaded/Sockets/Server Welcome.
R Some of these slides are from Prof Frank Lin SJSU. r Minor modifications are made. 1.
Object-Orientated Analysis, Design and Programming
Threads in Java Two ways to start a thread
MCA – 405 Elective –I (A) Java Programming & Technology
Client-server Programming
What happens when many clients want to contact the server at the same time ? The iterative approach consist in “enqueuing” the requests (this is done automatically.
An Introduction to Internetworking
An Introduction to Internetworking
Representation and Management of Data on the Internet
Implementing a concurrent file server (as an extension of Thread)
What happens when many clients want to contact the server at the same time ? The iterative approach consist in “enqueuing” the requests (this is done.
Polymorphism Sometimes it is necessary to “see” an object of a certain class as being (also) of another: Polymorphism An object can be seen as being of.
Presentation transcript:

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 1 What happens when many clients want to contact the server at the same time ?  The iterative approach consist in “enqueuing” the requests (this is done automatically by the system) and serving them one by one :  Accept the next connection and build the socket  Read request  Process request (send the response)  This will inevitably mean that if there is a client with a small request (for example, a small file request) will have to wait until the large requests are over  If more than the allowed number of clients at socket queue request a service during this time they are rejected !!!  There may be also some delays in attending clients by waiting some information (client are asked to type in something)  There are many Disk I/O operations in a file serving scenario which are normally “slow”and do not require network traffic or CPU  So there is a lot of resources standing idle with this schema

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 2 A sequential (iterative) server attending more than a client A SERVER A CLIENT 4444

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 3 During the transfer of the file the server cannot hear at the port 4444 for requests A SERVER A CLIENT 4444

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 4 Only after the transmition is over the server can attend another request A SERVER A CLIENT 4444

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 5 This may involve some delay (typing, transmitting large files) A SERVER A CLIENT 4444

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 6 Sometimes a large waiting time for the client may mean a timeot for the connection A SERVER A CLIENT 4444 Timeout

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 7 The Concurrent Server  A concurrent server can attend many clients at the same time  While transferring a file, it can still keep listening for requests  Every time a request comes, a new parallel line of statements execution is started for attending this request. After this the server can “hear” again at the server socket for further requests  Different approaches have been developed to implement parallel lines of executions within a program  The operative system plays an important role

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 8 In concurrent servers there is a main program which listens for client requests A SERVER A CLIENT 4444

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 9 After a client is contacted, a new line of execution is created which will attend the request in parallel. A SERVER A CLIENT 4444

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 10 The main program will return to the listening loop while the first client is being attended, so it can accept another request A SERVER A CLIENT 4444

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 11 … and create another parallel line of execution for attending the client A SERVER A CLIENT 4444

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 12 … so a request of a third client can be immediately be accepted A SERVER A CLIENT 4444

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 13 … thus having the three clients being attended in parallel A SERVER A CLIENT 4444

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 14 The Concurrent Server Algorithm Master program of the server 1. Create a server socket 2. Wait for and accept a client’s request 3. Create a parallel line of execution to attend the requirement of the new client 4. Goto 2. Slave (parallel) process : 1. Receive the parameters of the communication established by the master (socket or input/output data streams) 2. Attend client’s request (read filename, transfer data) 3. Return (disappear !)

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 15 Parallel Lines of execution  If there is only 1 CPU, why create parallel processes?  It often makes server programming easier.  Because there are more CPU involved !!!!!  The concept of parallel processes implemented at the S.O level appeared with UNIX and C.  In C a new process can be created by executing the fork() statement  int i = fork() creates an exact copy of the running process. Both continue executing the same program with the same variables.  For the “father” the value of i will be the id of the created process. For the child process this value will be 0  When programming concurrent servers the father will be the main process accepting requests and the child will process the client’s request

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 16 A simplified example of the use of for for implementing concurrent servers main() { int pid, msock, ssock; msock = passivesock(port, “tcp”, qlen); /* see chapter 10.4 from Internetworking with tcp/ip from Douglas Commer for the implementation */ while(1) { ssock = accept(msock, &fsin, &alen); pid = fork(); if (pid == 0) { process client’s request; return; }

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 17 Problems with fork() in UNIX  The creation of a new process is a time costly procedure  In some texts it is suggested to create a set of processes at the beginning which will be activated later by the main program. When a client arrives the parameters of the communication are passed vi pipes to child.  The new created process duplicates all the variables !!!  It is not easy to manage the processes which ended in an abnormal way. They keep the resources !!!.  Sometimes is preferred to use the “select” statement: it basically selects from an array of sockets the first one which has available data to read.

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 18 In JAVA is better to use Threads  A thread is a sequence or a flow of instructions which are executed in parallel to the main sequence of the program. It has a start and an end.  A thread can only be created and lives inside an already running process. A process can start as many threads as necessary.  Because of this, the main program has a better control of the threads it started. They can be created, started, suspended, or reactivated by the program.  Threads are implemented as methods of a certain class. When this method (normally called run) is activated, it starts to run in parallel to the method that called it.  As any other method, it can define its own set of variables

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 19 Using threads to implement concurrent servers Main (master) program: Create ServerSocket ss; While(true) { Socket s = ss.accept(); Create new thread with the socket s as parameter; start executing thread; } Define a thread class with a parallel method (run) which implements the processing of the clients request

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 20 Implementation of Threads  One way (perhaps the most clear one) is to define a new class which is an extension of the Thread class and overwrite its run() method.  Thread is an existing class  This thread class has a run method (originally with no statements) which will be executed in parallel when activated.  In order to execute this method a program must create an object of the extended class and apply the start() method to it.  The definition of the new Thread class should look like this:  public class MyThread extends Thread {  And somewhere should appear:  public void run() { //instructions to be executed in parallel }

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 21 Example public class SimpleThread extends Thread { public SimpleThread(String str) { super(str); } public void run() { for (int i = 0; i < 10; i++) { System.out.println(i + " " + getName()); try { this.sleep((int)(Math.random() * 1000)); } catch (InterruptedException e) {} } System.out.println("DONE! " + getName()); }  The this.sleep(miliseconds) must appear in a try and catch block

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 22 Use of this new class public class TwoThreadsTest { public static void main (String[] args) { SimpleThread t1,t2; t1 = SimpleThread("Jamaica"); t2 = SimpleThread("Fiji"); t1.start(); t2.start() }  The start() triggers the execution of the run method of the thread. There are other methods which can be applied to a thread: suspend(), resume(), stop(). These are however deprecated because their use can easily lead to deadlocks

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 23 Sometimes you cannot program the server as an extension of a thread  For example, if the server has to implement a graphical interface it should be declared as an extension of a Frame, if it is programmed as an Applet it must extend the Applet class  The we can use the interface Runnable, which means the Server will also be seen as a Runnable class and has to implement the run method run().  To initiate a parallel execution of the run method a Thread object must be created. In the creation a pointer to the server object (this) should be passed as parameter. With this, the Thread object will have access to the run method.  The run method implemented in the server class will be executed by performing start() on the new created Thread object.

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 24 Un repartidor de numeros N++ NoSyncronCliente Pide numero Entrega numero Pide numero Entrega numero NoSyncronCliente

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 25 Example with interface Runnable  See and execute the program NoSincron.java  Note that the Threads created in this way will have access to all resources of the master.  With the other form of creating threads, resources from the master object can be made available by passing a point to itself on creation of the thread

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 26 Critical regions and semaphores in Java  Java provides basically two methods for controlling concurrent access to resources  You can declare a whole method as a critical region (see sincron1)  You can use the semaphore of any object (see sincron2)  Another way to use semaphores in java is with wait() and notify()

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 27 Implementing a concurrent file server (as an extension of Thread)  Define a new Class as an extension of the Thread class which has a socket as Object variable.  Program a constructor which receives a socket as parameter.  Program the run methods to attend the client connected at the other extreme of the socket.  Program a main method which consists of an infinite while. Inside the while program the acceptance of a new client, the creation of a new thread with the socket obtained, and the starting of the thread execution.  The client does not change, in fact, it does not know if it is being served concurrently or in parallel  See: MultiArchServidorThread.java &MultiArchServidor.java

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 28 Broadcasting a text to many clients Hello

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 29 The server: receiving a new client 4444 Client contacts server at 4444

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 30 The server: receiving a new client 4444 A new socket is placed and the output channel is opened They are kept in an array

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 31 The server: Broadcasting a message When a message is entered the server will distribute it to the connected clients BroadcastCliente BraodcastServerNF Type a message

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 32 It is now easy to extend this to a chat system Hello

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 33 Conditions for implementing a chat system  Server must be listening to requests of new clients AND to messages which are sent by already connected  Client must be listening to messages from the server AND to the keyboard for messages the user wants to transmit.  There are many approaches for implementing this in TCP/IP  No one is the “absolute correct” solution, all them have their advantages and drawbacks  Normally (like everywhere in computers) “faster” solutions will use more memory

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 34 Solution 1  The server: 1.A thread listens to new clients trying to join the chat party 2.When a new clients connects, a PrintWriter and a BufferedReader for that client are created. The print Writer is kept in a vector 3.A thread is created and receives a pointer to the PrintWriters’ vector and the BufferedReader. It reads the input from the client and writes it to all PrintWriters (clients)  The client 1.Graphical interface for reading lines from the keyboard (there is a thread which triggers the execution of the actionPerformed method) and sends it to the server 2.A thread will read input from the server and display it on a text area  See: Chat1Server.java, Chat1ClientThread.java & Chat1Client

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 35 Solution 1 Schema Client1 Client2 Client3 New Client Thread PrintWriter BufferedReader

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 36 Solution 2  The server: 1.The server has only one thread listening at one port for attending all the requests of the clients. 2.The clients contact the server for registering (reg), sending a message (msg) or logging out (des) 3.The server keeps a PrintWiter vector to keep track about which clients participate in the chat party. Additionally, it keeps a vector with the nicknames of the clients  The client 1.Graphical interface for reading lines from the keyboard (there is a thread which triggers the execution of the actionPerformed method) and sends it to the server. It also shows the nicknames of the participants 2.A thread will read input from the server. This can be a message (msg) or a refreshment of the login list  See: CICChat.java & CICServer.java

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 37 Solution 2 Schema Client1 Client2 New Client

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 38 Solution 2 Client1 Client2 Client3

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 39 Peer-to-peer solution for TCP/IP  Every program should be client and server at the same time  When a new member wants to join the party, he/she shoud contact anyone of the group and ask for the list of contacts  After retrieving the list of contacts (hostnames or addresses) he/she should open an input and output channel with everyone (including the one who provided the list)

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 40 Peer-to-peer solution: a user starts a chat party by listening on a port for others wanting to join

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 41 A user wanting to join contacts the initiator. An InputStream and an OutputStream is opened on each

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 42 A third user may contact anyone and recover the list of participants, in this case each one has the address of the other in the list

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 43 Then contacts all of them opening Input/Output sreams. All them now have two entries on their participant list

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 44 Particularities of this implementation  The program is written in the ChatPtP.java file  In order to allow testing in one computer, it is necessary to give the port number at which the program will listen for newcomers  java ChatPtP localport starts a new Chat party  java ChatPtP remotehost remoteport localport joins an existing party (on remotehost at remoteport a program is waiting for new members)  The is one thread for listening to newcomers  Another listens for input on keyboard and sends it to all connected participants  There is one thread listening for input for each of the other participants  Not very easy, isn’t it ?

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 45 Problems of this implementation  What happens if one user leaves the chat party ?  What happens if during the second connection phase (retrieving the list and contacting the participants) another newcomer joins the party by asking a third one for the list ?  This is a very critical problem which has been more or less addressed in some (sometimes hard to understand) papers.

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 46 A concurrent web server  This will be implemented only for HTML files and classes (like servlets), but it is easily extensible for attending different types of requirements  For each client a new thread is created  The process is according to the type of the request HttpProcessor processRequest() HttpFile browser Httpd (server) thread HttpInputStream HttpOutputStream HttpClass HttpException Echo HttpClassProcessor

Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 47 Implementing state in a web server  The server must keep tracking of the different users that contact it  The client must submit this information  The Echo2 class keeps track of a “shopping cart” for different users  The client must submit a request like: user=username&product=productcode&qtty=number