Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a time-out limit to readLine()?

Tags:

java

sockets

pop3

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?

like image 497
Carven Avatar asked Oct 29 '11 07:10

Carven


1 Answers

I suggest you try Socket.setSoTime(timeout)

like image 109
Peter Lawrey Avatar answered Oct 10 '22 02:10

Peter Lawrey