gpt4 book ai didi

java - 在握手完成之前阻止 SSLSocket?

转载 作者:太空宇宙 更新时间:2023-11-03 12:38:25 25 4
gpt4 key购买 nike

有没有办法让 SSLSocket 在不使用 addHandshakeListener() 方法的情况下等待握手完成?我希望它在完成握手之前一直阻塞。

编辑:稍微澄清一下:最初使用常规(非 SSL)套接字(这是必需的,因为在实际握手发生之前必须将数据包发送到服务器)。使用 SSlSocketFactory.createSocket 将该套接字“转换”为 SSLSocket。 .我的问题是现在是否有办法同步进行握手。

最佳答案

SSLSocket.startHandshake() 是一个同步调用,当您第一次在对等端之间开始握手时,它将阻塞调用线程,直到握手完成:

... ...

// initial handshake between peers, don't need Listener:
sslSocket.startHandshake(); // <- this will block the calling thread.
// handshake is completed at this point

... ...

这在 Android API 中没有完整记录, 见原文 Java API对于完整的 javadoc:

Starts an SSL handshake on this connection. Common reasons include a need to use new encryption keys, to change cipher suites, or to initiate a new session. To force complete reauthentication, the current session could be invalidated before starting this handshake.

If data has already been sent on the connection, it continues to flow during this handshake. When the handshake completes, this will be signaled with an event. This method is synchronous for the initial handshake on a connection and returns when the negotiated handshake is complete. Some protocols may not support multiple handshakes on an existing socket and may throw an IOException.

如果你在对等点之间多次操纵握手,情况会变得相当复杂,在某些情况下(当你需要 Listener 时)它可能是一个异步调用,查看这个在线文档 Fundamental Networking in Java了解更多详情。

关于java - 在握手完成之前阻止 SSLSocket?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12956449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com