reset a3, kut Charlie ;)
[tt2015.git] / a3 / code / jtorx / server / Main.java
1 import java.net.InetAddress;
2
3 // Run the TCPServer on the the port testPort
4 public class Main {
5 private static final int DEFAULT_PORT = 10000;
6 private static final String DEFAULT_ADDRESS = "127.0.0.1";
7
8 /**
9 * Run the program with arguments to set a custom port and address, see the comments in the code
10 * @param args
11 * @throws Exception
12 */
13 public static void main(String[] args) throws Exception {
14 System.setProperty("java.net.preferIPv4Stack", "true"); // force ipv4
15 int port;
16 String address;
17 if (args.length == 0) {
18 // no arguments: default port and address
19 port = DEFAULT_PORT;
20 address = DEFAULT_ADDRESS;
21 } else if (args.length == 1) {
22 // one argument for port, default address
23 port = Integer.valueOf(args[0]);
24 address = DEFAULT_ADDRESS;
25 } else if (args.length == 2) {
26 // two arguments for port and address
27 // for example, call it like 'java Main 10000 127.0.0.1'
28 port = Integer.valueOf(args[0]);
29 address = args[1];
30 } else {
31 return;
32 }
33 TCPServer server = new TCPServer(port, InetAddress.getByName(address));
34
35 // comment this for the default handler, otherwise the echo server is used
36 server.setHandlerType("echo");
37
38 server.handleConnections();
39 }
40 }