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
Categories of I/O Devices
Advertisements

Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores.
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.
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.
Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
Distributed systems Programming with threads. Reviews on OS concepts Each process occupies a single address space.
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.
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)
© 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.
TCP Socket Programming CPSC 441 Department of Computer Science University of Calgary.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
An Introduction to Internetworking. Algorithm for client-server communication with UDP (connectionless) A SERVER A CLIENT Create a server-socket (listener)and.
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)
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
1 Chapter Client-Server Interaction. 2 Functionality  Transport layer and layers below  Basic communication  Reliability  Application layer.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Threads and Processes.
12/1/98 COP 4020 Programming Languages Parallel Programming in Ada and Java Gregory A. Riccardi Department of Computer Science Florida State University.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
1 Web Based Programming Section 8 James King 12 August 2003.
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.
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.
RMI Remote Method Invocation Distributed Object-based System and RPC Together 2-Jun-16.
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.
Li Tak Sing COMPS311F. RMI callbacks In previous example, only the client can initiate a communication with the server. The server can only response to.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
Java Server Programming Web Interface for Java Programs.
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.
Object Serialization.  When the data was output to disk, certain information was lost, such as the type of each value.  If the value "3" is read from.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Multi-Threading in Java
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
Prepared by Dr. Jiying Zhao University of Ottawa Canada.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp Advanced Java Programming CSE 7345/5345/ NTU 531 Multithreaded/Sockets/Server Welcome.
Threads in Java Two ways to start a thread
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
Gentle Introduction to Threads
Concurrency, Processes and Threads
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.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
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  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);