SSL 套接字连接超时

发布于 2024-11-02 09:33:36 字数 527 浏览 0 评论 0原文

如何在 Java 中配置 SSL 套接字的连接超时?

对于普通套接字,我可以使用 new Socket() 创建没有任何目标端点的新套接字实例,然后调用 connect(SocketAddress 端点, int timeout) 方法。使用 SSL 套接字,我无法创建没有端点的 new SSLSocket()SSLSocketFactory.getDefault().createSocket() 方法,并会抛出 UnsupportedOperationException 和 < strong>未连接的套接字未实现消息。

有没有办法在 Java 中使用 SSL 套接字的连接超时,仅使用标准 java 库?

How can I configure connect timeout for SSL Sockets in Java?

For plain sockets, I can simply create new socket instance without any target endpoint using new Socket(), and then call connect(SocketAddress endpoint, int timeout) method. With SSL sockets, I cannot create new SSLSocket() and SSLSocketFactory.getDefault().createSocket() method with no endpoint throws UnsupportedOperationException with Unconnected sockets not implemented message.

Is there a way to use connect timeouts for SSL Sockets in Java, using standard java libs only?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

提笔书几行 2024-11-09 09:33:36

我相信您可以使用当前创建 Socket 然后连接它的方法。要通过连接建立SSL,您可以使用SSLSocketFactory.createSocket

返回一个分层的套接字
连接到指定的现有套接字
主机,在给定的端口。

这样您就可以完全控制连接,然后您可以在其上协商设置 SSL。如果我误读了你的问题,请告诉我。

I believe you could use your current approach of creating the Socket and then connecting it. To establish SSL over the connection you could use SSLSocketFactory.createSocket

Returns a socket layered over an
existing socket connected to the named
host, at the given port.

This way you get full control over the connection and then you negociate setting up SSL on top of it. Please let me know if I misread your question.

苦行僧 2024-11-09 09:33:36

对于 java 1.7,以下内容不会引发问题中所述的异常:

String host = "example.com";
int port = 12345;
int connectTimeout = 5000;
SSLSocket socket = (SSLSocket)SSLSocketFactory.getDefault().createSocket();
socket.connect(new InetSocketAddress(host, port), connectTimeout);
socket.startHandshake();

因此一切照常。

With java 1.7 the following does not throw the exception stated in the question:

String host = "example.com";
int port = 12345;
int connectTimeout = 5000;
SSLSocket socket = (SSLSocket)SSLSocketFactory.getDefault().createSocket();
socket.connect(new InetSocketAddress(host, port), connectTimeout);
socket.startHandshake();

so it's business as usual.

夜巴黎 2024-11-09 09:33:36

详细说明@predi的答案,我发现我也需要使用“setSoTimeout”。否则有时它会卡在握手中(在非常不稳定的连接上):

    final int connectTimeout = 30 * 1000;
    SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().createSocket();
    socket.setSoTimeout(connectTimeout);
    socket.connect(new InetSocketAddress(hostAddress, port), connectTimeout);
    socket.startHandshake();
    socket.setSoTimeout(0);`

Elaborating on @predi's answer, I found that I needed to use "setSoTimeout" too. Otherwise sometimes it gets stuck in the handshake (on very unstable connections):

    final int connectTimeout = 30 * 1000;
    SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().createSocket();
    socket.setSoTimeout(connectTimeout);
    socket.connect(new InetSocketAddress(hostAddress, port), connectTimeout);
    socket.startHandshake();
    socket.setSoTimeout(0);`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文