Actividad Sockets

Post on 08-Nov-2014

19 views 1 download

Transcript of Actividad Sockets

ACTIVIDAD Clases URL e InetAddressGUSTAVO ANDRES OSPINO REALES

1065598997

Sockets

Programa Cliente

import java.io.*;import java.net.*;class Cliente { static final String HOST = "localhost"; static final int PUERTO=5000; public Cliente( ) { try{

Socket NuevoCliente = new Socket( HOST , PUERTO);InputStream aux = NuevoCliente.getInputStream();DataInputStream flujo = new DataInputStream( aux );System.out.println( flujo.readUTF() );NuevoCliente.close();

} catch( Exception e ) {System.out.println( e.getMessage() );}

} public static void main( String[] arg ) { new Cliente(); }}

Programa Servidor

import java.io.* ;import java.net.* ;

class Servidor { static final int PUERTO=5000;

public Servidor( ) { try { ServerSocket NuevoServidor = new ServerSocket( PUERTO ); System.out.println("Comunicando por el puerto " + PUERTO ); for ( int numCli = 0; numCli < 3; numCli++) { Socket NuevoCliente = NuevoServidor.accept(); // Crea objeto System.out.println("Servicio al cliente " + numCli); OutputStream aux = NuevoCliente.getOutputStream(); DataOutputStream flujo= new DataOutputStream( aux );

flujo.writeUTF( "Hola cliente " + numCli ); NuevoCliente.close();

} System.out.println("Suficientes clientes por hoy"); } catch( Exception e ) {

System.out.println( e.getMessage() );

}

} public static void main( String[] arg ) { new Servidor(); }

}