ݺߣ
Submit Search
Sockets java
•
1 like
•
324 views
Giovani Hernandez
Follow
ejemplo sencillo de conexion via sockets en java con shell de sistema
Read less
Read more
1 of 3
Download now
Download to read offline
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 ) {
3.
new Servidor(); } }
Download