/** * Listens for connections from the opposing parties with lower id's. * * @throws IOException thrown if an {@link IOException} occurs while listening. */ private Map<Integer, Socket> connectServer(final NetworkConfiguration conf) throws IOException { Map<Integer, Socket> socketMap = new HashMap<>(conf.getMyId() - 1); if (conf.getMyId() > 1) { try (ServerSocket server = serverFactory.createServerSocket(conf.getMe().getPort())) { logger.info("P{}: bound at port {}", conf.getMyId(), conf.getMe().getPort()); for (int i = 1; i < conf.getMyId(); i++) { Socket sock = server.accept(); int id = 0; for (int j = 0; j < PARTY_ID_BYTES; j++) { id ^= sock.getInputStream().read() << j * Byte.SIZE; } socketMap.put(id, sock); logger.info("P{}: accepted connection from P{}", conf.getMyId(), id); socketMap.put(id, sock); } } } return socketMap; }
@Test(expected = RuntimeException.class, timeout = TWO_MINUTE_TIMEOUT_MILLIS) public void testFailToBind() throws Throwable { ServerSocket socket = null; try { List<NetworkConfiguration> confs = getNetConfs(2); socket = ServerSocketFactory.getDefault().createServerSocket(confs.get(1).getMe().getPort()); newCloseableNetwork(confs.get(1)); } finally { socket.close(); } }