简体   繁体   中英

How to established a connection and not closing it after passing some time

Im developing a TCP socket in Java, and I want to make the connection between server and client open all the time until it has passed some time, and then the connection close and wait for a new accept, how can I implement it by a while with a timer? Also, can I make that if the client or the server has not made any coomunication after passing some time, then the connection close? I try to do it by setSoTimeout, but it only works with the accept, so is there any method that allows you to close the connection after some time?

Try using socket.settimeout(time). Alternatively, get the system time when the socket was opened, and the current system time, and if the difference is a specific amount of time, close the socket.

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