ConnectionTimeout 与 SocketTimeout

发布于 2024-12-04 04:07:00 字数 544 浏览 1 评论 0原文

我正在使用的库有问题。可能是图书馆的问题,也可能是我用错了!

基本上,当我这样做时(超时以毫秒为单位)

_ignitedHttp.setConnectionTimeout(1);  // v short
_ignitedHttp.setSocketTimeout(60000);  // 60 seconds

不会生成超时异常并且工作正常,但是,当我执行以下操作时,

_ignitedHttp.setConnectionTimeout(60000);  // 60 seconds
_ignitedHttp.setSocketTimeout(1);          // v short

我收到套接字异常。

所以,我的问题是为什么我不能模拟连接异常?我是否误解了套接字和连接超时之间的区别?该库位于此处(尚未正式发布)。

I'm having a problem with a library that I am using. It might be the library or it might be me using it wrong!

Basically, when I do this (Timeout in milliseconds)

_ignitedHttp.setConnectionTimeout(1);  // v short
_ignitedHttp.setSocketTimeout(60000);  // 60 seconds

No timeout exception is generated and it works ok, however, when I do the following,

_ignitedHttp.setConnectionTimeout(60000);  // 60 seconds
_ignitedHttp.setSocketTimeout(1);          // v short

I get a Socket Exception.

So, my question is why can I not simulate a Connection Exception? Am I misunderstanding the difference between a socket and a connection time-out? The library is here (not officially released yet).

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

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

发布评论

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

评论(2

身边 2024-12-11 04:07:00

连接超时

连接超时仅在启动 TCP 连接时发生。如果远程计算机没有应答,通常会发生这种情况。如果您收到 ConnectException,可能的原因是:服务器已关闭、您使用了错误的 IP/DNS 名称、错误的端口或与服务器的网络连接已关闭。

套接字超时

套接字超时专用于监视连续传入的数据流。如果数据流中断指定时间,则连接被视为已停止/中断。当然,这仅适用于始终接收数据并且延迟时间不超过配置的套接字超时的连接。

通过将套接字超时设置为 1000(毫秒),这将要求每秒接收一次新数据(假设您以块方式读取数据并且块足够大)。

如果只有传入流停止超过一秒,您就会遇到套接字超时。

当 HTTP 服务器处理复杂的请求(需要在服务器端花费一些时间才能获得 HTTP 响应数据)时,这一点尤其重要。如果您将套接字超时配置为 10000(10 秒),但 HTTP 服务器在收到 HTTP 请求后需要 15 秒,那么您将永远不会收到响应,因为 10 秒后您将收到 SocketTimeoutException (无数据)在 HTTP 请求的接收之间传输,直到 HTTP 响应准备好)。

Connection Timeout

A connection timeout occurs only upon starting the TCP connection. This usually happens if the remote machine does not answer. If you get an ConnectException, possible reasons are: the server has been shut down, you used the wrong IP/DNS name, wrong port or the network connection to the server is down.

Socket Timeout

A socket timeout is dedicated to monitor the continuous incoming data flow. If the data flow is interrupted for the specified time the connection is considered as stalled/broken. Of course this only works with connections where data is received all the time and there are no delays longer than the configured socket timeout.

By setting socket timeout to 1000 (ms) this would require that every second new data is received (assuming that you read the data block wise and the block is large enough).

If only the incoming stream stalls for more than a second you are running into a socket timeout.

This is especially important when HTTP servers process a complex request that requires some time on server side before the HTTP response data is available. If you configure socket timeout to 10000 (10 seconds) but the HTTP server requires 15 seconds after receiving the HTTP request, then you will never get the response as after 10 seconds you will get an SocketTimeoutException (no data is transmitted between reception of the HTTP request until the HTTP response is ready).

2024-12-11 04:07:00

连接超时是程序愿意等待建立与另一个进程的连接的最长时间。此时您没有获取或发布任何应用程序数据,只是建立连接本身。

套接字超时是等待单个数据包时的超时。一个常见的误解是套接字超时是接收完整响应的超时。因此,如果套接字超时为 1 秒,响应由 3 个 IP 数据包组成,其中每个响应数据包需要 0.9 秒才能到达,总响应时间为 2.7 秒,则不会出现超时。

A connection timeout is the maximum amount of time that the program is willing to wait to setup a connection to another process. You aren't getting or posting any application data at this point, just establishing the connection, itself.

A socket timeout is the timeout when waiting for individual packets. It's a common misconception that a socket timeout is the timeout to receive the full response. So if you have a socket timeout of 1 second, and a response comprised of 3 IP packets, where each response packet takes 0.9 seconds to arrive, for a total response time of 2.7 seconds, then there will be no timeout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文