如何使 Java Socket 的读取超时?

发布于 2024-09-16 04:37:14 字数 95 浏览 2 评论 0原文

我正在尝试从套接字读取项目,我注意到如果套接字流上没有任何内容,它将停留在读取状态并备份我的应用程序。我想知道是否有办法设置读取超时或在套接字中没有任何内容一定时间后终止连接。

I'm trying to read items from a socket and I notice that if there is nothing on the stream of the socket it will stay at the read and back up my application. I wanted to know if there was a way to set a read timeout or terminate the connection after a certain amount of time of nothing in the socket.

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

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

发布评论

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

评论(3

心头的小情儿 2024-09-23 04:37:14

如果您编写 Java,请学习浏览API 文档有帮助。在套接字读取的情况下,您可以 设置超时选项, 例如:

socket.setSoTimeout(500);

这将导致与套接字关联的 InputStreamread() 之后抛出 SocketTimeoutException code> 调用阻塞半秒。需要注意的是,SocketTimeoutException 在此类 read() 调用引发的异常中是唯一的,因为套接字仍然有效;您可以继续使用它。异常只是一种逃避读取并决定是否需要做不同事情的机制。

while (true) {
    int n;
    try {
        n = input.read(buffer);
    catch (SocketTimeoutException ex) {
        /* Test if this action has been cancelled */
        if (Thread.interrupted()) throw new InterruptedIOException();
    }
    /* Handle input... */
}

If you write Java, learning to navigate the API documentation is helpful. In the case of a socket read, you can set the timeout option, e.g.:

socket.setSoTimeout(500);

This will cause the InputStream associated with the socket to throw a SocketTimeoutException after a read() call blocks for one-half second. It's important to note that SocketTimeoutException is unique among exceptions thrown by such read() calls, because the socket is still valid; you can continue to use it. The exception is only a mechanism to escape from the read and decide if it's time to do something different.

while (true) {
    int n;
    try {
        n = input.read(buffer);
    catch (SocketTimeoutException ex) {
        /* Test if this action has been cancelled */
        if (Thread.interrupted()) throw new InterruptedIOException();
    }
    /* Handle input... */
}
窝囊感情。 2024-09-23 04:37:14

如果此套接字是通过 URLConnection 创建的以执行 Web 请求,则可以在读取流之前直接在 URLConnection 上设置读取和连接超时:

InputStream createInputStreamForUriString(String uriString) throws IOException, URISyntaxException {
    URLConnection in = new URL(uriString).openConnection();
    in.setConnectTimeout(5000);
    in.setReadTimeout(5000);
    in.setAllowUserInteraction(false);
    in.setDoInput(true);
    in.setDoOutput(false);
    return in.getInputStream();
}

If this socket was created through a URLConnection to perform a web request, you can set the read and connect timeouts directly on the URLConnection before reading the stream:

InputStream createInputStreamForUriString(String uriString) throws IOException, URISyntaxException {
    URLConnection in = new URL(uriString).openConnection();
    in.setConnectTimeout(5000);
    in.setReadTimeout(5000);
    in.setAllowUserInteraction(false);
    in.setDoInput(true);
    in.setDoOutput(false);
    return in.getInputStream();
}
隐诗 2024-09-23 04:37:14

是的,应该重写 Read() 来接受超时值。通过“覆盖”,我并不是建议任何人编写一个,而是指出他正在使用的套接字方法的覆盖之一需要超时值。

Yes, there should be an override of Read() that accepts a timeout value. By 'override' I am not suggesting anyone write one, I am pointing out that one of the overrides of the socket methods he is using takes a timeout value.

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