ݺߣ

ݺߣShare a Scribd company logo
Cliente java
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();
}
}
Servidor
import java.io.* ;
import java.net.* ;
import java.net.ServerSocket;
public 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();
}
}

More Related Content

Sockets java

  • 1. Cliente java 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(); } }
  • 2. Servidor import java.io.* ; import java.net.* ; import java.net.ServerSocket; public 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 ) {