如何在Java Socket上超时读取? [英] How to timeout a read on Java Socket?

查看:140
本文介绍了如何在Java Socket上超时读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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.

这篇关于如何在Java Socket上超时读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆