首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SSLEngine和close

SSLEngine和close
EN

Stack Overflow用户
提问于 2009-05-28 15:44:05
回答 2查看 2.1K关注 0票数 2

我实现了一个帮助模块,它允许我从SSL使用的通道获取干净的数据,并将加密的数据写入其中:这是相关的接口(我在该类中也有一些非抽象的方法,所以没有对我说"DataProvider应该是一个接口“):

代码语言:javascript
复制
public abstract class DataProvider {
    // Notify the bytes read from the net
    public abstract void readFromNet(ByteBuffer b);
    // Gets the bytes to write into the net
    public abstract void writeToNet(ByteBuffer b);
    // Add a message to send
    public abstract boolean addMessage(byte[] data);
    // Obtains the application data received
    public abstract byte[] getReceivedApplicationData();
    // True if there is something to actually send over the wire
    public abstract boolean dataToSend();
    // True if we should close the channel
    public abstract boolean shouldClose();
    // Notify our intention to shut down the connection
    public abstract void shutDown(SelectionKey sk);
    // Set the interest op set for the channel
    public abstract void setInterestOps(SelectionKey sk);
}

我有一个SSL抽象基类的实现。在测试该实现时,我编写了两个函数:一个是用SocketChannel接收消息,用于发送数据的SSLSocket关闭连接,另一个是用SocketChannel初始化关闭连接的消息。现在,问题是用于接收数据的SSLSocket没有关闭,即使我已经发布了这些步骤:

  1. engine.closeOutbound()
  2. engine.wrap()
  3. channel.write(data) (是的,我确信我已经发送了wrap()
  4. 获得的所有数据-用于读取入站close_notify

的通道的选择)

问题是选择器被卡住在第四步。

在另一个测试(SSLSocket关闭连接)中,我没有问题。

请注意,我已经将shouldClose实现为:

代码语言:javascript
复制
return engine.isOutboundDone() && engine.isInboundDone();

因此,我需要一个传入的close_notify来关闭,即使我已经初始化了关闭(我不知道这是否正确:最终我可以用return engine.isOutboundDone()更改它)

这是我的代码的SSLSocket端:

代码语言:javascript
复制
Socket toRead = socket.accept();
toRead.setSoTimeout(0);
InputStream is = toRead.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
String read = "";
byte[] barray = new byte[1024];
while (read.length() < toSend.length() * 2) {
    int bytesRead = is.read(barray);
    bos.write(barray, 0, bytesRead);
    read = new String(bos.toByteArray());
}
assertEquals(toSend + toSend, read);
assertTrue(toRead.isClosed());

最后一个断言被违反了。

最初,我认为这是因为没有与toRead相关的“后台”线程,所以我应该使用它进行读/写,以便使用传入的close_notify,然后最终关闭套接字,但即使这样也没有帮助。

有什么想法吗?

EN

回答 2

Stack Overflow用户

发布于 2011-07-17 02:21:29

非常晚的答复,但您不需要等待close_notify,如果您已经发送了一个,请参阅RFC 2246。不过,你应该买一个。你没有说你是如何在close_notify上进行选择的。

你的代码没有意义。只有关闭套接字时,才会关闭套接字。isClosed()指的是套接字,而不是连接。

票数 3
EN

Stack Overflow用户

发布于 2009-06-04 17:37:30

我建议在第一个断言之前添加is.close();

请注意,我建议的这段代码并没有关闭套接字。它应该关闭InputStream。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/921579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档