简体   繁体   中英

TCP Server/Client with Timeout

I have created a client and server that communicates to one another properly, but I'm having a problem implementing the timeout feature. If I don't receive any input from the client for 5 seconds, then I should notify the client. How do I tackle this problem so that waiting for the request is concurrent with the rest of the program?

BufferedReader inFromClient = new BufferedReader(  
            new InputStreamReader(connectionSocket.getInputStream()));   
...
String clientMove = inFromClient.readLine();
//this is where I wait 5 seconds of a response

Thanks in advance!

Use Socket.setSoTimeout() and catch SocketTimeoutException , but why the message to the client? What is the client supposed to do with an unsolicited timeout message? Will it really be listening for that? Or will it be busy sending the message it is supposed to be sending?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM