Download presentation
Presentation is loading. Please wait.
1
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - Email: hthiemer @ cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 1 Programming the Server What happens on the server when the client tries to establish a rendezvous ? The server starts listening to requests on a ServerSocket After accepting the request the resulting connection is attached to another (normal) socket (same type as client’s socket)
2
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - Email: hthiemer @ cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 2 Sockets at the Server Side (1) The server should start by creating a server socket bound to a certain port according to the protocol of the service. ServerSocket listening; listening = new ServerSocket(5555); This will create the socket but the server is still not listening. To do this we should apply the following method to the socket: Socket toClient = listening.accept(); This sentence works the following way: accept() blocks the execution of the program until a request for a rendezvous from a client is received. When the requirement arrives, a tcp connection is established between the two computers. The client receives in its socket one end of this link and the server the other. The server side’s socket (from the Socket class) is chosen conveniently by the system
3
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - Email: hthiemer @ cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 3 Sockets at the Server Side(2) At the server side we can apply the same methods to the “normal” socket as we did at the client side. Particularly we may need to open an input and an output data stream. After this, the server should implement the communication protocol which was established and published (by any other possible mean). It is important that both side follow this protocol in order not to block the communication and/or miss some data. This mean nothing else than following the “turn taking” rules of writing to and reading from the socket and the format of the data to be exchanged. Note that the server socket (and port) at which the server was originally listening to requests is not used anymore. This is a design issue.
4
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - Email: hthiemer @ cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 4 We will program now a date server for a computer which has no one (for example, a MS-Windows computer) A Date Server Date server 13 Client 1) Create the server socket 2) start listening 4) close the connection 3) answer with the date in another socket DateServer DateClient2
5
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - Email: hthiemer @ cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 5 We will program now a date server for a computer which has no one (for example, a MS-Windows computer) An Echo Server Date server 7 Client 1) Create the server socket 2) start listening 3) Request a line 4) answer with the same Do 3 & 4 until client disconnects or sends a line with ** EchoServer EchoClient2
6
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - Email: hthiemer @ cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 6 Bla bla from keyboard Talk client Talk Server Let’s program something rather simple Bla bla The TalkServer waits for someone wishing to communicate The TalkClient asks for a host name and tries the redezvous After the communication is set up, everything the client user types in will be transmitted to the talk server and this will display it on the screenboard
7
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - Email: hthiemer @ cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 7 Let’s see another example of simple client-server communication with TCP Open server socket port =4444 While(true) { accept call open reading from socket while (true) { read line from socket if (line.equals(“bye”)) break; write line to screen } //end of the call } s=new Socket(args[0],4444) open writing to socket while (true) { read line from keyboard write to socket if (line.equals(“bye”)) break; } TalkClient TalkServer
8
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - Email: hthiemer @ cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 8 Sockets: File transfer (1) We will now develop programs for transmitting files. The one receiving the file starts listening for someone who wants to transmit a file (the server !!!) The sender knows where (hostname and port number) the server is listening and sends a rendezvous request. The data transfer is done at the byte level in order to allow the transfer of non textual files.
9
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - Email: hthiemer @ cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 9 2)The sender tries a rendezvous with receiver 4) Send bytes 3) Read bytes from file5) Write bytes in file Repeat 3,4,5 until all the file is transmitted Transmitting files 1)The reciver starts listening for Requests to send (upload) files See ArchEnviador.java ArchRecibidor.java
10
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - Email: hthiemer @ cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 10 1) Filename from keyboard 2) Request file 4) Send file 3) Read File5) Write file Repeat 3,4,5 until all the file is transmitted A more intelligent file server See ArchServer.java ArchCliente.java
11
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - Email: hthiemer @ cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 11 Stateless vs. Stateful servers: the problem of reading a remote file by steps. File reading requests arrive with delay A SERVER A CLIENT Open file XYZ read first 50 bytes while (not end of file XYZ) read next 50 bytes close file ? Request open file XYZ Answer file XYZ exists and ready
12
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - Email: hthiemer @ cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 12 A stateless server means it does not remember previous requests A SERVER A CLIENT Open file XYZ read first 50 bytes while (not end of file XYZ) read next 50 bytes close file ? Request read bytes 0 to 49 from file XYZ Answer the content of the bytes
13
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - Email: hthiemer @ cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 13 The client must provide all the information again ! A SERVER A CLIENT Open file XYZ read first 50 bytes while (not end of file XYZ) read next 50 bytes close file ? Request read bytes 50 to 99 from file XYZ Answer the content of the bytes
14
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - Email: hthiemer @ cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 14 This may cause a lot of network traffic, especially if there are many clients A SERVER A CLIENT Open file XYZ read first 50 bytes while (not end of file XYZ) read next 50 bytes close file ? Request read bytes X to X+50 from file XYZ Answer the content of the bytes
15
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - Email: hthiemer @ cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 15 Stateful Server: it maintains some information abut what clients did A SERVER A CLIENT Open file XYZ read first 50 bytes while (not end of file XYZ) read next 50 bytes close file ? Request open file XYZ Answer: file pointer to file XYZ Pointer File Position 0 XYZ 0 1 FILE ZXY 50
16
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - Email: hthiemer @ cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 16 The information the client has to pass to the server is much smaller A SERVER A CLIENT Open file XYZ read first 50 bytes while (not end of file XYZ) read next 50 bytes close file ? Request 0, read 50 Answer: the content Pointer File Position 0 XYZ 50 1 FILE ZXY 50
17
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - Email: hthiemer @ cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 17 The information at the server should be updated with every request A SERVER A CLIENT Open file XYZ read first 50 bytes while (not end of file XYZ) read next 50 bytes close file ? Request 0, read 50 Answer: the content Pointer File Position 0 XYZ 100 1 FILE ZXY 50
18
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - Email: hthiemer @ cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 18 It is important to close the file !!! A SERVER A CLIENT Open file XYZ read first 50 bytes while (not end of file XYZ) read next 50 bytes close file ? Request 0, read 50 Answer: the content Pointer File Position 0 XYZ 100 1 FILE ZXY 50
19
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - Email: hthiemer @ cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 19 Communications: the networked approach Every 2 applications which want to start a communication open an exclusive channel (hostnames and ports mut be known to each other) We have n*(n-1)/2 channels for n applicatons Advantages: An exclusive channel, no bottlenecks Drawbacks: Every application must be aware of port and host address. Management of incomming/outgoing members
20
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - Email: hthiemer @ cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 20 The centralized (star) approach Applications send their communications requirements to a server. This will forward the message to the final destinatary. We have at most n communication channels Advanatages: Easier management of the communication Drawbacks: Server saturation.
21
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - Email: hthiemer @ cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 21 Software Arquitecture: replicated applications In a replicated schema an instance of the application is started at every place The applications may differ from each other (since they are started independently) to support different roles.
22
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) 698 8427 - Email: hthiemer @ cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores 22 Software Arquitecture: centralized application In a centralized schema there is only one application which sends it outputs to the screen (window server program) of all computers It also collects the input from all them Net meeting is a famous example of this approach Advantages and Drawbacks ?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.