Setting a timeout limit for readLine ()?

I have the following code that reads a response from a POP server through Sockets in Java. But sometimes a problem arises when I use the readLine () function to read from the server, and if the server does not respond 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 other ways in which the server does not respond after a certain time, the application should stop waiting for a response and continue its execution?

+6
source share
1 answer

I suggest you try Socket.setSoTime (timeout)

+4
source

Source: https://habr.com/ru/post/900332/


All Articles