1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.net.time;
19
20 import java.io.DataOutputStream;
21 import java.io.IOException;
22 import java.net.ServerSocket;
23 import java.net.Socket;
24
25
26
27
28
29
30
31
32
33 public class TimeTestSimpleServer implements Runnable {
34
35
36
37
38 public static final long SECONDS_1900_TO_1970 = 2208988800L;
39
40
41 public static final int DEFAULT_PORT = 37;
42
43 public static void main(final String[] args) {
44 final TimeTestSimpleServer server = new TimeTestSimpleServer();
45 try {
46 server.start();
47 } catch (final IOException e) {
48
49 }
50 }
51
52 private ServerSocket server;
53 private final int port;
54
55 private boolean running;
56
57 public TimeTestSimpleServer() {
58 port = DEFAULT_PORT;
59 }
60
61 public TimeTestSimpleServer(final int port) {
62 this.port = port;
63 }
64
65 public void connect() throws IOException {
66 if (server == null) {
67 server = new ServerSocket(port);
68 }
69 }
70
71 public int getPort() {
72 return server == null ? port : server.getLocalPort();
73 }
74
75 public boolean isRunning() {
76 return running;
77 }
78
79 @Override
80 public void run() {
81 Socket socket = null;
82 while (running) {
83 try {
84 socket = server.accept();
85 final DataOutputStream os = new DataOutputStream(socket.getOutputStream());
86
87 final int time = (int) ((System.currentTimeMillis() + 500) / 1000 + SECONDS_1900_TO_1970);
88 os.writeInt(time);
89 os.flush();
90 } catch (final IOException e) {
91
92 } finally {
93 if (socket != null) {
94 try {
95 socket.close();
96 } catch (final IOException e) {
97 System.err.println("close socket error: " + e);
98 }
99 }
100 }
101 }
102 }
103
104
105
106
107 public void start() throws IOException {
108 if (server == null) {
109 connect();
110 }
111 if (!running) {
112 running = true;
113 new Thread(this).start();
114 }
115 }
116
117
118
119
120 public void stop() {
121 running = false;
122 if (server != null) {
123 try {
124 server.close();
125 } catch (final IOException e) {
126 System.err.println("close socket error: " + e);
127 }
128 server = null;
129 }
130 }
131
132 }