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 There may be also some delays in attending clients by waiting some information (clients 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 transfering 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 In JAVA it is easy to use Threads A thread is a secuence or a flow of instructions which are executed parallel to the main secuence 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 16 Using threads to implement concurrent servers Main (master) program: Create ServerSocket sSocket; While(true) { Socket socket = sSocket.accept(); Create new thread with the socket “socket” 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 17 Implementation of Threads One way (perhaps the most clear one) is to define a new class which is an extention 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() { //instrucction 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 18 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(milisegundos) 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 19 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 20 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 21 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 server. De la otra forma es más bien una opción, (pasar un puntero al servidor cuando se crea un objeto thread) De cualquier forma, será frecuente el compartir recursos y por lo tanto la admistración de ellos es importante
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 22 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 (ver sincron1) You can use the semaphore of any object (ver sincron2)
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 23 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. MultiFileServer MultiFileThread
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 24 Talk clientTalk Server Now we can program a telephone in one program Bla bla Talk clientTalk Server Bla bla Everyone can initiate a call to anyone When answering a call the calling function must be dissalowed When originating a call the the answering function must be dissallowed For now, we will only allow the caller to send messages
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 25 State Diagram of the Phone Both threads running 2- Stop the thread waiting for a call and make the call 3- Stop the thread for making a call and answer 4- reed from keyboard and write to socket 5- read from socket and write on the screen Receive a call Make a call End communication
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 26 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 27 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 28 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 29 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 30 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 31 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. We need in the server 2 server sockets (and a different thread attending each one) We need at the client a server socket and a thread to attend it
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 32 The architecture of the system BroadcastCliente2 BraodcastServer The client registers 2.Server put another OS in the vector 3.User types a message 4.Send message to server 5.Server broadcast it 6.Client prints it
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 33 Transmitting Objects via TCP Transmision: marshaling, delivery & unmarshaling. The key for this is the Object Serialization: convert the into a representation which can be transmitted across the net (String) All native Java Objects are serializables. For user defined objects it is necesary to write implements Serializable in their definition (this does not include static variables or references to local things such as files or sockets)
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 34 Transmitting Objects via TCP The classes which allows their transmition: ObjectInputStream readObjetct() ObjectOutputStream writeObject() The user can change the “standard” serialization mechanism by declaring implements Externalizable This means, the user must implement Void writeExternal(ObjectOutputStram o) Void readExternal(ObjectInputStream i);