KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > TestServer


1 package test;
2 import java.io.*;
3 import java.net.*;
4
5 /**
6   Server to used perform tests for SOCKS library.
7 */

8 public class TestServer implements Runnable JavaDoc{
9   static PrintStream log = null;
10
11   int service;
12
13
14   /**
15    Creates a TestServer object which will listen on the port associated
16    with given service.
17
18    @param service Service to provide
19   */

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   //Static functions
33
/////////////////
34

35
36   /**
37     Listens on the port associated with given service.
38     <p>
39     When connection is accepted, speciefied service is performed.
40     It is being done in separate thread.
41     @return Never returns.
42   */

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 JavaDoc t = new Thread JavaDoc(st);
51     t.start();
52     s = ss.accept();
53      }
54   }
55
56
57   /**
58     Performs logging.
59   */

60   static synchronized void log(String JavaDoc s){
61      if(log != null) log.println(s);
62   }
63
64   //Main Function
65
///////////////
66
public static void main(String JavaDoc[] 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 JavaDoc t = new Thread JavaDoc(st);
76      t.start();
77       }
78   }
79 }
80
Popular Tags