Ciao!
ho fatto un piccolo server che fa due stupidate messe in croce (praticamente data una stringa, la converte in maiuscolo).
Il passo successivo è stato quello di renderlo multitrhead, in modo tale da far collegare più client alla volta.....
Ora vorrei fare questo....vorrei praticamente aprire contemporaneamente più server su porte diverse, ma il mio problema risiede nel fatto che quando invoco il metodo go(), ovviamente questo blocca il chiamante e non mi fa partire gli altri server....
se qualcuno può darmi una mano...
posto qui di seguito il codice
codice:package serverthreaded; /** * * @author Albe */ import java.net.*; import java.io.*; import java.util.*; public class ServerThreaded_1 { int port1 = 7979; // int port2 =7000; public ServerThreaded_1(int p) { port1 = p; // port2 = p; } /** * It never terminates. */ public void go() throws IOException { ServerSocket s = new ServerSocket(port1); //ServerSocket k = new ServerSocket(port2); System.out.println("Multithreaded Server started on port " + port1); // System.out.println("Multithreaded Server started on port " + port2); Socket client = null; while (true) { try { client = s.accept(); Thread t = new ServerHandler(client); t.start(); } catch (IOException e) { if (client != null) client.close(); } } } public static void main(String args[]) throws IOException { ServerThreaded_1 srv = new ServerThreaded_1(7979); // ServerThreaded_1 srv1 = new ServerThreaded_1(7000); srv.go(); // srv1.go(); } } class ServerHandler extends Thread { Socket cln; ServerHandler(Socket c) {cln = c;} public void run() { BufferedReader in = null; PrintStream out = null; Date prima = new Date(); System.out.println("Connessione avvenuta alle ore: " + prima.toString()); try { in = new BufferedReader( new InputStreamReader( cln.getInputStream())); out = new PrintStream(cln.getOutputStream()); boolean done = false; while(! done) { String str = in.readLine(); if (str == null) done = true; else { String str1 = str.toUpperCase(); out.println(str1); if(str1.equals("END")) done = true; } } Date dopo = new Date(); System.out.println("Disconnessione avvenuta alle ore: " + dopo.toString()); } catch (IOException e) { System.out.println("Server thread exiting because of IOException..."); } finally { try { out.close(); in.close(); } catch (IOException e) { } } } }
io quindi nel main vorrei poter fare qualcosa del genere:
ServerThreaded_1 srv = new ServerThreaded_1(7979);
ServerThreaded_1 srv1 = new ServerThreaded_1(7000);
ServerThreaded_1 srv2 = new ServerThreaded_1(9000);
ServerThreaded_1 srv3 = new ServerThreaded_1(9999);
srv.go();
srv1.go();
srv2.go();
srv3.go();
...e così via.....
Grazie ancora!