1 import java
.net
.InetAddress
;
3 // Run the TCPServer on the the port testPort
5 private static final int DEFAULT_PORT
= 10000;
6 private static final String DEFAULT_ADDRESS
= "127.0.0.1";
9 * Run the program with arguments to set a custom port and address, see the comments in the code
13 public static void main(String
[] args
) throws Exception
{
14 System
.setProperty("java.net.preferIPv4Stack", "true"); // force ipv4
17 if (args
.length
== 0) {
18 // no arguments: default port and address
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]);
33 TCPServer server
= new TCPServer(port
, InetAddress
.getByName(address
));
35 // comment this for the default handler, otherwise the echo server is used
36 server
.setHandlerType("echo");
38 server
.handleConnections();