01: package test;
02:
03: import java.io.*;
04: import java.net.*;
05:
06: /**
07: Server to used perform tests for SOCKS library.
08: */
09: public class TestServer implements Runnable {
10: static PrintStream log = null;
11:
12: int service;
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:
33: //Static functions
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(
45: TestService.servicePorts[service]);
46: Socket s;
47:
48: s = ss.accept();
49: while (s != null) {
50: TestService st = new TestService(s, service);
51: Thread t = new Thread(st);
52: t.start();
53: s = ss.accept();
54: }
55: }
56:
57: /**
58: Performs logging.
59: */
60: static synchronized void log(String s) {
61: if (log != null)
62: log.println(s);
63: }
64:
65: //Main Function
66: ///////////////
67: public static void main(String[] args) {
68: log = System.out;
69: TestService.log = log;
70:
71: TestServer st;
72: for (int i = 0; i < TestService.serviceNames.length; ++i) {
73: log("Starting service " + TestService.serviceNames[i]
74: + " at port " + TestService.servicePorts[i] + ".");
75: st = new TestServer(i);
76: Thread t = new Thread(st);
77: t.start();
78: }
79: }
80: }
|