设置readLine()的超时限制? [英] Setting a time-out limit to readLine()?

查看:563
本文介绍了设置readLine()的超时限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码通过Java中的套接字读取POP服务器的响应。但问题有时候,当我使用readLine()函数从服务器读取时,如果服务器没有回复任何响应,我的应用程序将挂起,等待服务器的响应。

I have the following code that reads response from a POP server through Sockets in Java. But the problem is sometimes, when I use the readLine() function to read from the server and if the server does not reply with any response, my application will hang there, waiting for a response from the server.

socket.connect(new InetSocketAddress("pop.server.com", 110), 3000);
input = socket.getInputStream();
BufferedReader incoming = new BufferedReader(new InputStreamReader(input));
incoming.readLine();   //This line will cause my application to hang if the server does not respond with a reply

是否存在一种设置超时的方法或一些其他方式,当服务器在一段时间后没有回复时,应用程序应该停止等待响应并继续执行其他操作?

Is there a way to set a timeout or some other ways that when the server does not reply after a certain amount of time, the application should stop waiting for a response and continue its other execution?

推荐答案

我建议你试试 Socket.setSoTime(超时)

这篇关于设置readLine()的超时限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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