Servidor a varios clientes con JAVA
disculpen mi improligidad en el mensaje anterior...
para los que no sabe Socket con java les voy a dar las bases para hacer un programa servidor con java.
tenemos la clase Socket(). está sirve tanto para crear un cliente o un servidor, supuestamente ya sabe el rol que cumple cada uno. (si no pregunten). bueno un Servidor escuchando seria de esta manera
Usando Sockets Java [FONT=Courier New, monospace]
Socket s = new Socket(host,puerto);//creo un servidor
Socket incoming=s.accept(); // lo pongo a escuchar por ese puerto. cuando haya un cliente este le dara el paso.
para hacerlo mas dificultoso podriamos hacerlo para varios clientes no? que es lo que seria ideal.
eso se puede hacer muy facil con nuestros Thread
algo asi:
[/FONT] Servicio a varios clientes Servicio a varios clientes [FONT=Courier New, monospace]
public class ThreadedEchoServer{ [/FONT]
[FONT=Courier New, monospace]
public static String reverse(String linea){ [/FONT]
[FONT=Courier New, monospace]
String revLine = "";[/FONT]
[FONT=Courier New, monospace]
for(int i = linea.length()-1;i>=0;i--)[/FONT]
[FONT=Courier New, monospace]
revLine+=linea.charAt(i);[/FONT]
[FONT=Courier New, monospace]
return revLine; [/FONT]
[FONT=Courier New, monospace]
} [/FONT]
[FONT=Courier New, monospace]
public static void main(String[] args ){[/FONT]
[FONT=Courier New, monospace]
try{ [/FONT]
[FONT=Courier New, monospace]
int i = 1;[/FONT]
[FONT=Courier New, monospace]
ServerSocket s = new ServerSocket(8189);[/FONT]
[FONT=Courier New, monospace]
for (;{ [/FONT]
[FONT=Courier New, monospace]
Socket incoming = s.accept( );[/FONT]
[FONT=Courier New, monospace]
System.out.println("Spawning " + i);[/FONT]
[FONT=Courier New, monospace]
Thread t = new ThreadedEchoHandler(incoming, i);[/FONT]
[FONT=Courier New, monospace]
t.start();[/FONT]
[FONT=Courier New, monospace]
i++;[/FONT]
[FONT=Courier New, monospace]
}[/FONT]
[FONT=Courier New, monospace]
}catch (Exception e){e.printStackTrace();}[/FONT]
[FONT=Courier New, monospace]
}[/FONT]
[FONT=Courier New, monospace]
}[/FONT]
[FONT=Courier New, monospace]
[/FONT] Servicio a varios clientes [FONT=Courier New, monospace]
class ThreadedEchoHandler extends Thread{ [/FONT]
[FONT=Courier New, monospace]
private Socket incoming;[/FONT]
[FONT=Courier New, monospace]
private int counter;[/FONT]
[FONT=Courier New, monospace]
public ThreadedEchoHandler(Socket i, int c){ [/FONT]
[FONT=Courier New, monospace]
incoming = i; counter = c; [/FONT]
[FONT=Courier New, monospace]
}[/FONT]
[FONT=Courier New, monospace]
public void run(){ [/FONT]
[FONT=Courier New, monospace]
try{ [/FONT]
[FONT=Courier New, monospace]
BufferedReader in = new BufferedReader[/FONT]
[FONT=Courier New, monospace]
(new InputStreamReader(incoming.getInputStream()));[/FONT]
[FONT=Courier New, monospace]
PrintWriter out = new PrintWriter[/FONT]
[FONT=Courier New, monospace]
(incoming.getOutputStream(), true /* autoFlush */);[/FONT]
[FONT=Courier New, monospace]
out.println( "Holala! Adios para irse..." );[/FONT]
[FONT=Courier New, monospace]
boolean done = false;[/FONT]
[FONT=Courier New, monospace]
while (!done){ [/FONT]
[FONT=Courier New, monospace]
String str = in.readLine();[/FONT]
[FONT=Courier New, monospace]
if (str == null) done = true;[/FONT]
[FONT=Courier New, monospace]
else{ [/FONT]
[FONT=Courier New, monospace]
out.println("Eco del hilo " + counter + " " + str);[/FONT]
[FONT=Courier New, monospace]
out.println("Rev. " + ThreadedEchoServer.reverse(str));[/FONT]
[FONT=Courier New, monospace]
if (str.trim().equals("Adios"))[/FONT]
[FONT=Courier New, monospace]
done = true;[/FONT]
[FONT=Courier New, monospace]
}[/FONT]
[FONT=Courier New, monospace]
}[/FONT]
[FONT=Courier New, monospace]
incoming.close();[/FONT]
[FONT=Courier New, monospace]
}catch (Exception e){e.printStackTrace();}[/FONT]
[FONT=Courier New, monospace]
}[/FONT]
[FONT=Courier New, monospace]
}[/FONT]
[FONT=Courier New, monospace]
espero que les haya servido a modo de explicacion simple de Servidores con java se puede hacer infinidades de cosas y quiero que alguien si puede me pueda hacer un servlet con java que es un poco mas complicado.
Suerte y son muy buenos los temas que estan tocando no hay que desperdiciar ninguno. chau:-p
[/FONT]