简体   繁体   中英

How to get idle Time of Socket connection?

I am using ServerSocket in my java application and it will be listening to port 1111. When the client connects to the port each client connection will be executed in separate Thread. Each connection will receive data for every 1 minute until the connection is closed.

The question is, suppose if the client connection doesn't receive any data for more than 10 minutes some event should be triggered and the important thing is connection should remain unclosed. So that i can use that event to execute an mail alert to me.

Is there any technique to do this?

You can implement a Listener or Observer Interface. Here is the algorithm. Create a Thread, eg., ClientMonitor which polls a flag in the client object. You can also use a TimerTask to poll the flag/object at specified time intervals

  1. Start a new TimerTask to monitor the client
  2. Check if the client has received any data in last ten minutes.
  3. If yes, then reset the TimerTask .
  4. If no, then raise an Exception , which will take care of executing the mail alert while the connection to the client remains open.
  5. Reset the TimerTask

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