Bienvenido a Tecnohackers

Tecnohackers » Programacion » Area de Programacion » Programacion a Alto Nivel. Visual Basic, Java, delphi, etc.
 » 

Conexion Simple de Cliente-Servidor por Sockets



Autor Tema: Conexion Simple de Cliente-Servidor por Sockets  (Leído 1306 veces)

Desconectado kaiser

  • Veterano
  • ***
  • Mensajes: 250
    • http://foro.latinohack.com
Conexion Simple de Cliente-Servidor por Sockets
« en: Febrero 04, 2009, 04:36:01 am »
El codigo es muy sencillo lo unico que hace el servidor es, cuando un cliente le pide el servicio, lo saludo y avisa que servio al cliente

Servidor

Código: You are not allowed to view links. Register or Login
import java.io.* ;
import java.net.* ;

class Servidor {
    static final int PUERTO = 80;
   
    public Servidor() {
        try {
            ServerSocket Servidor = new ServerSocket(PUERTO);
            while(true) {
                Socket Cliente = Servidor.accept();
                System.out.println("Atencion servi a un cliente");
                OutputStream os = Cliente.getOutputStream();
                DataOutputStream dot= new DataOutputStream(os);
                dot.writeUTF("Buenas tardes señor cliente, espero que se encuentre bien");
                Cliente.close();
            }
        }
        catch(Exception err) {
            System.out.println(err.getMessage());
        }
    }
   
    public static void main(String[] arg) {
        new Servidor();
    }
}


Cliente

Código: You are not allowed to view links. Register or Login
import java.io.*;
import java.net.*;

class Cliente {
    static final String HOST = "localhost";
    static final int Puerto = 80;
   
    public Cliente() {
        try{
            Socket Cliente = new Socket(HOST , Puerto);
            InputStream is = Cliente.getInputStream();
            DataInputStream dis = new DataInputStream(is);
            System.out.println(dis.readUTF());
            Cliente.close();
        }
        catch(Exception err) {
            System.out.println(err.getMessage());
        }
    }
   
    public static void main(String[] arg) {
        new Cliente();
    }
}


Y aca un .bat que constantemente llama al servidor

Código: You are not allowed to view links. Register or Login
@echo off
:a
java Cliente
goto a

Tags:
Tags:

 


SMF 2.0.19 | SMF © 2016, Simple Machines
Paginas Afiliadas
Twitter - FaceBook - Daraxblog
Designed by Smf Personal