簡體   English   中英

設置readLine()的超時限制?

[英]Setting a time-out limit to readLine()?

我有以下代碼通過Java中的套接字讀取POP服務器的響應。 但問題是,當我使用readLine()函數從服務器讀取並且服務器沒有回復任何響應時,我的應用程序將掛起,等待服務器的響應。

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

有沒有辦法設置超時或其他一些方法,當服務器在一段時間后沒有回復時,應用程序應該停止等待響應並繼續其他執行?

我建議你試試Socket.setSoTime(超時)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM