int port = config.getPort();
FreenetInetAddress bindto = config.getBindTo();
UdpSocketHandler u = null;
if(port > 65535) {
throw new NodeInitException(NodeInitException.EXIT_IMPOSSIBLE_USM_PORT, "Impossible port number: "+port);
} else if(port == -1) {
// Pick a random port
for(int i=0;i<200000;i++) {
int portNo = 1024 + random.nextInt(65535-1024);
try {
u = new UdpSocketHandler(portNo, bindto.getAddress(), node, startupTime, getTitle(portNo), node.collector);
port = u.getPortNumber();
break;
} catch (Exception e) {
Logger.normal(this, "Could not use port: "+bindto+ ':' +portNo+": "+e, e);
System.err.println("Could not use port: "+bindto+ ':' +portNo+": "+e);
e.printStackTrace();
continue;
}
}
if(u == null)
throw new NodeInitException(NodeInitException.EXIT_NO_AVAILABLE_UDP_PORTS, "Could not find an available UDP port number for FNP (none specified)");
} else {
try {
u = new UdpSocketHandler(port, bindto.getAddress(), node, startupTime, getTitle(port), node.collector);
} catch (Exception e) {
Logger.error(this, "Caught "+e, e);
System.err.println(e);
e.printStackTrace();
throw new NodeInitException(NodeInitException.EXIT_IMPOSSIBLE_USM_PORT, "Could not bind to port: "+port+" (node already running?)");