sábado, 17 de noviembre de 2007

Codigo java para servidor y cliente corregido

El servidor:

import java.io.* ;
import java.net.* ;
class Servidor {
static final int PUERTO=5000;
public Servidor( ) {
try {
ServerSocket skServidor = new ServerSocket( PUERTO );
System.out.println("Escucho el puerto " + PUERTO );
for ( int numCli = 0; numCli < 3; numCli++ ) {
Socket skCliente = skServidor.accept(); // Crea objeto
System.out.println("Sirvo al cliente " + numCli);
OutputStream aux = skCliente.getOutputStream();
DataOutputStream flujo= new DataOutputStream( aux );
flujo.writeUTF( "Hola cliente " + numCli );
skCliente.close();
}
System.out.println("Demasiados clientes por hoy");
} catch( Exception e ) {
System.out.println( e.getMessage() );
}
}
public static void main( String[] arg ) {
new Servidor();
}
}



El cliente

import java.io.*;
import java.net.*;
class Cliente {
static final String HOST = "localhost";
static final int Puerto=5000;
public Cliente( ) {
try{
Socket skCliente = new Socket( HOST , Puerto );
InputStream aux = skCliente.getInputStream();
DataInputStream flujo = new DataInputStream( aux );
System.out.println( flujo.readUTF() );
skCliente.close();
} catch( Exception e ) {
System.out.println( e.getMessage() );
}
}
public static void main( String[] arg ) {
new Cliente();
}
}

No hay comentarios:

Publicar un comentario