Java套接字超时不起作用

时间:2016-11-03 14:02:10

标签: java sockets socket-timeout-exception

我是一个负责监听其他两台具有完全相同类别的机器的课程,因此它是具有相同代码的三台计算机的网络。连接在那里,我可以看到它们相互传递数据。一切都工作正常。

当我取出其中一台机器并观察其他两台机器的行为时,事情变得棘手。预计,当其中一台机器出于某种原因停止工作时,其他两台机器应继续运行。如果其中两个停止,剩下的就应该继续。

我尝试在下面实现这种机制。但是,当我取出其中一台机器时,程序会一直等待,所以它不会切换到"双向比较模式"。

SIGNATURE

为节省您的时间,请允许我就此事分享我的意见。我怀疑这部分存在问题:

public void listen() {
    try {
    logger.info("Creating listener sockets");

    while (isRunning) {
        final byte[] buf = new byte[bufferSize];

        final DatagramPacket packetOne = new DatagramPacket(buf, buf.length);
        final DatagramPacket packetTwo = new DatagramPacket(buf, buf.length);
        MediatorMessageMsg mediatorMessageOne = null;
        MediatorMessageMsg mediatorMessageTwo = null;

        try {
            socketReceiverOne.receive(packetOne);
            ByteArrayInputStream firstInput = new ByteArrayInputStream(buf);
            mediatorMessageOne = MediatorMessageMsg.parseDelimitedFrom(firstInput);

            socketReceiverTwo.receive(packetTwo);
            ByteArrayInputStream secondInput = new ByteArrayInputStream(buf);
            mediatorMessageTwo = MediatorMessageMsg.parseDelimitedFrom(secondInput);

            logger.trace("Received packets");
        } catch (final SocketTimeoutException e) {
            logger.trace(e.getMessage());
            continue;
        } catch (final SocketException e) {
            logger.warn(e);
            logger.warn("Ignore the error and go on.");
            continue;
        } catch (final IOException e) {
            logger.error("Incoming communication stopped!");
            logger.error(e);
            stop();
        }

        // if two mediators sent the data, it's OK
        if (packetOne.getLength() > 0 && packetTwo.getLength() > 0) {
            handlePackets(mediatorMessageOne, mediatorMessageTwo);
            logger.info("Number of active mediators: 2. Comparison style: 1v1v1");
        }
        // if only one sent the data, compare it with our own
        else if (packetOne.getLength() > 0 || packetTwo.getLength() > 0) {
            // whicehever sent the data, compare its data with our own
            logger.info("Number of active mediators: 1. Comparison style: 1v1");
            if (packetOne.getLength() > 0) {
                handlePackets(mediatorMessageOne);
            } else {
                handlePackets(mediatorMessageTwo);
            }

        }
        // if no data is sent, then pass our own directly
        else {
            logger.info("Number of active mediators: 0. Comparison style: No Comparison");
            // our datamodel to retrieve data on our own
            DataModel modelOwn = DataModel.getInstance();
            MediatorMessageMsg newMessage = MediatorMessageMsg.newBuilder().setHeading(modelOwn.getHeading()).setSpeed(modelOwn.getSpeed()).setSender(getId()).build();
            // publish(topicName, newMessage);
        }

        Thread.sleep(1);
    }

    socketReceiverOne.close();
    socketReceiverTwo.close();
    logger.info("stopped");

} catch (final IllegalArgumentException e) {
    logger.error("Illegal argument received: " + e);
} catch (final Exception e) {
    logger.error("Unexpected error occured: " + e);
} finally {
    if (socketReceiverOne instanceof DatagramSocket && socketReceiverTwo instanceof DatagramSocket) {
        if (!socketReceiverOne.isClosed() || !socketReceiverTwo.isClosed()) {
            socketReceiverOne.close();
            socketReceiverTwo.close();
        }
    }
}

}

对我而言,似乎程序需要一个包,当它无法接收时,它会一直等待。虽然我有超时的例外条件,但我无法完成。

            socketReceiverOne.receive(packetOne);
            ByteArrayInputStream firstInput = new ByteArrayInputStream(buf);
            mediatorMessageOne = MediatorMessageMsg.parseDelimitedFrom(firstInput);

            socketReceiverTwo.receive(packetTwo);
            ByteArrayInputStream secondInput = new ByteArrayInputStream(buf);
            mediatorMessageTwo = MediatorMessageMsg.parseDelimitedFrom(secondInput);

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

好的,我发现了我错的地方。我需要更多端口(进出)。一旦我合并了这些端口,问题就不再发生了。