1 package test; 2 import java.io.*; 3 import java.net.*; 4 5 8 public class TestServer implements Runnable { 9 static PrintStream log = null; 10 11 int service; 12 13 14 20 public TestServer(int service){ 21 this.service = service; 22 } 23 24 public void run(){ 25 try{ 26 server(service); 27 }catch(IOException ioe){ 28 log("Exception:"+ioe); 29 ioe.printStackTrace(); 30 } 31 } 32 35 36 43 static public void server(int service) throws IOException{ 44 ServerSocket ss = new ServerSocket(TestService.servicePorts[service]); 45 Socket s; 46 47 s = ss.accept(); 48 while(s!=null){ 49 TestService st = new TestService(s,service); 50 Thread t = new Thread (st); 51 t.start(); 52 s = ss.accept(); 53 } 54 } 55 56 57 60 static synchronized void log(String s){ 61 if(log != null) log.println(s); 62 } 63 64 public static void main(String [] args){ 67 log = System.out; 68 TestService.log = log; 69 70 TestServer st; 71 for( int i = 0; i< TestService.serviceNames.length;++i){ 72 log("Starting service "+TestService.serviceNames[i]+" at port "+ 73 TestService.servicePorts[i]+"."); 74 st = new TestServer(i); 75 Thread t = new Thread (st); 76 t.start(); 77 } 78 } 79 } 80 | Popular Tags |