Вы находитесь на странице: 1из 17

Investigacin del Modelo Cliente-Servidor Qu es un modelo cliente servidor

Es un sistema de aplicacin distribuida, el cual se presenta como un grupo de procesos en cooperacin, llamados servidores los cuales proveen de recursos a entidades demandantes denominadas clientes; normalmente tanto el servidor como el cliente ejecutan el mismo micronucleo ejecutandose ambos como procesos del usuario. Caractersticas de un cliente y de un servidor

Cliente Es las instancia que inicia las peticiones para obtener acceso a los recursos. Recibe los recursos del servidor Se conecta a varios servidores simultaneamente. Es el que interactua con los usuarios mediante una GUI (Guide User Interface Interfaz Grfica de Usuario) Servidor: Espera peticiones de los clientes, por lo que su papel dentro de un sistema distribuido es pasivo. Al recibir una peticin, este la procesa y la envia de regreso con el recurso pedido. No interactua con los usuarios directamente.

Qu es la arquitectura multicapas

Dado que el modelo cliente-servidor consta de 2 capas, hay otras que sus nodos de red disponen de 3 tipos de capas, los cuales son :

Los clientes que interactuan con los usuarios y envien peticiones al servidor Los Servidores de aplicacin que procesan las peticiones de los clientes y regresan el recurso solicitado Los servidores de datos, los cuales almacenan informacin para entregarla a los servidores de aplicacin.

Ventajas Los procesos se separan de los servidores, balanceando las cargas de estos y agilizando la entrega de recursos a los clientes. Tienen una mejor escalabilidad

Desventajas El trafico en la red se incrementa Es ms complicada la programacin de este modelo, ya que debe establecerse comunicacin con ms dispositivos.

Ejemplos Un ejemplo es el uso de un cajero para disposicin de efectivo, ya que el cliente solicita los estados de cuenta del usuario, para entregar una cantidad de dinero la cual es requerida por el usuario; este ultimo emplea una estacin en una central bancaria o centro comercial para efectuar la peticin, el cajero enlaza con el micro-sistema del servidor el cual ejecuta los procesos para consultar en los servidores de datos la cantidad de la que dispone el usuario, por ultimo el servidor entrega al cliente la autorizacin para efectuar la entrega de papel moneda al usuario final.

Segunda parte de Investigacin b. En qu lenguaje de programacin se puede desarrollar una aplicacin cliente servidor? R= Realmente se puede hacer en casi cualquier lenguage, pero los ms comunes son: JAVA C++ Perl

PHP Python ASP.net Visual Basic

c. Qu tipo de protocolo de comunicacin se utiliza en este modelo? Uno de los protocolos ms comunes para utilizar cliente-servidor es TCP, pero dependiendo del tipo de servicio puede ser. RDP (3389) FTP (21) Telnet (23) Secure Shell (22) Netbios (445) Http (80 y 8080) VNC (5900) VPN (1723)

d. Presenta un diagrama del modelo cliente servidor

e. Presenta un ejemplo de cdigo en el modelo cliente servidor codificado en Java en el que se muestre la manera en que el cliente utiliza un servidor para copiar un archivo y un ejemplo de cdigo de la manera en que se realiza el direccionamiento en un servidor.

Codigo de Ejemplo : clase Server.java import java.io.EOFException; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.ServerSocket; import java.net.Socket; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.SwingUtilities;

public class Server extends JFrame { private JTextField enterField; private JTextArea displayArea; private ObjectOutputStream output; private ObjectInputStream input;

private ServerSocket server; private Socket connection; private int counter = 1;

public Server() { super( "Server" );

enterField = new JTextField(); enterField.setEditable( false ); enterField.addActionListener( new ActionListener() {

public void actionPerformed( ActionEvent event ) { sendData( event.getActionCommand() ); enterField.setText( "" ); } } );

add( enterField, BorderLayout.NORTH );

displayArea = new JTextArea();

add( new JScrollPane( displayArea ), BorderLayout.CENTER );

setSize( 300, 150 ); setVisible( true ); }

public void runServer() { try { server = new ServerSocket( 12345, 100 );

while ( true ) { try { waitForConnection(); getStreams(); processConnection(); } catch ( EOFException eofException ) { displayMessage( "\nServer terminated connection" ); } finally {

closeConnection(); counter++; } } } catch ( IOException ioException ) { ioException.printStackTrace(); } }

private void waitForConnection() throws IOException { displayMessage( "Waiting for connection\n" ); connection = server.accept(); connection displayMessage( "Connection " + counter + " received from: " + connection.getInetAddress().getHostName() ); }

private void getStreams() throws IOException {

output = new ObjectOutputStream( connection.getOutputStream() ); output.flush();

input = new ObjectInputStream( connection.getInputStream() );

displayMessage( "\nGot I/O streams\n" ); }

private void processConnection() throws IOException { String message = "Connection successful"; sendData( message );

setTextFieldEditable( true );

do { try { message = ( String ) input.readObject(); displayMessage( "\n" + message ); catch ( ClassNotFoundException classNotFoundException ) { displayMessage( "\nUnknown object type received" ); }

} while ( !message.equals( "CLIENT>>> TERMINATE" ) );

private void closeConnection() { displayMessage( "\nTerminating connection\n" ); setTextFieldEditable( false );

try { output.close(); input.close(); connection.close(); } catch ( IOException ioException ) { ioException.printStackTrace(); } }

private void sendData( String message ) { try { output.writeObject( "SERVER>>> " + message );

output.flush(); displayMessage( "\nSERVER>>> " + message ); } catch ( IOException ioException ) { displayArea.append( "\nError writing object" ); } }

private void displayMessage( final String messageToDisplay ) {

Clase Client.java import java.io.EOFException; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.InetAddress; import java.net.Socket; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea;

import javax.swing.JTextField; import javax.swing.SwingUtilities;

public class Client extends JFrame { private JTextField enterField; private JTextArea displayArea; private ObjectOutputStream output; private ObjectInputStream input; private String message = ""; private String chatServer; private Socket client;

public Client( String host ) { super( "Client" );

chatServer = host;

enterField = new JTextField(); enterField.setEditable( false ); enterField.addActionListener( new ActionListener() {

public void actionPerformed( ActionEvent event )

{ sendData( event.getActionCommand() ); enterField.setText( "" ); } } );

add( enterField, BorderLayout.NORTH );

displayArea = new JTextArea(); add( new JScrollPane( displayArea ), BorderLayout.CENTER );

setSize( 300, 150 ); setVisible( true ); }

public void runClient() { try { connectToServer(); getStreams(); processConnection(); } catch ( EOFException eofException )

{ displayMessage( "\nClient terminated connection" ); } catch ( IOException ioException ) { ioException.printStackTrace(); } finally { closeConnection(); } }

private void connectToServer() throws IOException { displayMessage( "Attempting connection\n" );

client = new Socket( InetAddress.getByName( chatServer ), 12345 );

displayMessage( "Connected to: " + client.getInetAddress().getHostName() ); }

private void getStreams() throws IOException

output = new ObjectOutputStream( client.getOutputStream() ); output.flush();

input = new ObjectInputStream( client.getInputStream() );

displayMessage( "\nGot I/O streams\n" ); }

private void processConnection() throws IOException {

setTextFieldEditable( true );

do { try { message = ( String ) input.readObject(); displayMessage( "\n" + message ); } catch ( ClassNotFoundException classNotFoundException ) {

displayMessage( "\nUnknown object type received" ); }

} while ( !message.equals( "SERVER>>> TERMINATE" ) ); }

private void closeConnection() { displayMessage( "\nClosing connection" ); setTextFieldEditable( false );

try { output.close(); input.close(); client.close(); } catch ( IOException ioException ) { ioException.printStackTrace(); } }

// send message to server private void sendData( String message ) {

try { output.writeObject( "CLIENT>>> " + message ); output.flush(); displayMessage( "\nCLIENT>>> " + message ); } catch ( IOException ioException ) { displayArea.append( "\nError writing object" ); } }

private void displayMessage( final String messageToDisplay ) { SwingUtilities.invokeLater( new Runnable() { public void run() { displayArea.append( messageToDisplay ); } } ); }

private void setTextFieldEditable( final boolean editable ) { SwingUtilities.invokeLater( new Runnable() { public void run() { enterField.setEditable( editable ); } } ); } }

Bibliografa: Thanenbaum, Adrew S. Y Maarten Van Steen (2008), Sistemas Distribuidos: Principios y Paradigmas, P. p. 17-50, Segunda Edicin, Mxico: Pearson Educacin Deitel, Paul. J y Deitel Harvey M. (2008), Como programar en JAVA, Capitulo 24: Redes, P.p. 994-1014, Mxico: Pearson prentice hall, Septima Edicin. (Ejemplo de cdigo Fuente)

Вам также может понравиться