1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.net;
15
16 import java.io.InputStream;
17 import java.net.ServerSocket;
18 import java.net.Socket;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 public class ExternalMockSocketServer {
23
24 static final String LOGGINGEVENT = "LoggingEvent";
25 static final String LOGGINGEVENT2 = "LoggingEvent2";
26 static final String MINIMALEXT = "MinimalExt";
27 static final String MINIMALSER = "MinimalSer";
28
29 static final int PORT = 4560;
30
31
32 static int clientNumber;
33
34 static List<String> msgList = new ArrayList<String>();
35 static boolean finished = false;
36
37 String className = LOGGINGEVENT;
38
39 public static void main(String[] args) {
40 if (args.length == 1) {
41 clientNumber = Integer.parseInt(args[0]);
42
43 runServer();
44 } else {
45 usage("Wrong number of arguments.");
46 }
47 }
48
49 static void usage(String msg) {
50 System.err.println(msg);
51 System.err.println("Usage: java " + ExternalMockSocketServer.class.getName() + " loopNumber");
52 System.exit(1);
53 }
54
55 static void runServer() {
56
57 try {
58 System.out.println("Starting Server...");
59 ServerSocket serverSocket = new ServerSocket(PORT);
60 System.out.println("Listening on port " + PORT);
61 for (int j = 0; j < clientNumber; j++) {
62 Socket socket = serverSocket.accept();
63 System.out.println("New client accepted.");
64 System.out.println("Connected to client at " + socket.getInetAddress());
65
66 InputStream is = socket.getInputStream();
67 long sum = 0;
68
69 while (true) {
70
71 int val = is.read();
72 if (val == -1) {
73 break;
74 }
75
76
77 int a = is.available();
78 sum += a + 1;
79 is.skip(a);
80 }
81 System.out.println(sum / 1000 + " KB");
82 }
83 serverSocket.close();
84 } catch (Exception se) {
85 se.printStackTrace();
86 }
87 System.out.println("Server finished.");
88 finished = true;
89 }
90
91 }