【问题标题】:CipherOutputStream does not flush data to socket output streamCipherOutputStream 不会将数据刷新到套接字输出流
【发布时间】:2014-06-16 19:01:33
【问题描述】:

所以我的代码应该将浮点数发送到服务器并从中接收整数。但事实并非如此。显然flush方法有问题,数据似乎没有进入服务器。在我的代码中,socket.output()/input() 和 _output/_input 返回包装到 DataStreams 中的 CipherStreams 以建立连接。 safeInstance 方法建立连接并创建密码。客户端中的 OuputStream 和服务器中的 InputStream 包含一个 int(0) 和带有客户端十六进制版本公钥的 UTF 字符串。密钥是相同的。是否有一些解决方案在发送数据后不关闭流(或关闭后更新它的方法)?使用 AES 加密。

在客户端我们有

try {
    UtilDHSocket socket = UtilDHSocket.safeInstance(0);
    socket.output().writeFloat(1.0F);
    socket.output().flush();
    int answer = socket.input().readInt();
    socket.input().close();
    socket.output().close();
    socket.close();
    return answer == 200;
} catch(Exception ex) {
    error(ex);
    return false;
}

在服务器端也有类似的东西

try {
    float clientVersion = _input.readFloat();
    if(clientVersion == Main._version) {
        _output.writeInt(200);
    } else {
        _output.writeInt(418);
    }
    _output.flush();
    _input.close();
    _output.close();
    _client.close();
} catch(Exception ex) {
    warning("Unable to process packet form ",this._client.getInetAddress().getHostAddress());
    debug(ex);
}

【问题讨论】:

    标签: java sockets encryption outputstream


    【解决方案1】:

    关闭套接字的输入流或输出流会关闭另一个流和套接字。因此,您有两个多余的关闭。移除输入流和套接字的闭包。那么输出流的闭包就可以对流做它需要做的事情了。

    【讨论】:

    • 感谢您的回答。但它甚至无法达到close语句。它停留在 input.read() 语句。
    • 您在问题中没有提到“卡在input.read()”,也没有提到您是否真的尝试过这个建议,两端。
    猜你喜欢
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 2014-08-10
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 2015-01-01
    相关资源
    最近更新 更多