Note that there are some explanatory texts on larger screens.

plurals
  1. POHold timer type thread
    text
    copied!<p>im having difficulties figuring this out for myself hence asking here. i have an application which installs a client on a remote machine and the client reports back through a socket.</p> <p>im trying to create a hold timer. which means the client will send a hello message every 1 minute to the server.. if the server doesnt get a hello packet from the client within the hold timer the client is removed. i can do it fine with one client... but for multiple i cannot figure it out. i was thinking to create a thread foreach new client that connected to the server. and then inside that thread start a hold timer.. but how do i distinguish clients from one another with the threads and how do i reset the hold timer IF a hello packet is recieved from a client.</p> <p>i thought of creating a new thread for every hello packet and stopping the old one. but i dont know how cpu intensive this will be</p> <p>if you dont understand my question, please say so and ill try to explain whatever you dont understand.</p> <p>which solution is better? Solution A) starting a new thread and stopping the old thread everytime a hello packet arrives? (every 40 secs) - from 100 clients</p> <p>Solution B) insert much better more scalable solution here.</p> <p>Solution C) can i access a timer inside a thread i create dynamically? - i have unique threadnames on all dynamically created threads.</p> <pre><code>public void _clientholdtimer() { holdtimer = new Timer(); holdtimer.Interval = SECtoMS(Settings.Default.Hold); holdtimer.Elapsed += holdtimer_Elapsed; } </code></pre> <p>and reset it when i recieve a hello packet from client with same name as thread?</p> <p>Solution D) store the timers in a dictionary public Dictionary timersDict = new Dictionary(); and the loop find the reference in the dict on hello packet recieved reset that timer.?</p> <pre><code>foreach(var item in timersDict) { if(item.Key == stripip(client.RemoteEndPoint.ToString())) TimerReset(item.Value); } </code></pre> <p><strong>SOLUTION</strong></p> <p>Setup up the remote host application, to send a "hello" message to the Server every 3 seconds. Create a timer on the server with an elapsed event hit every 1 sec. setup a static value to hold an int which determines, how long before I consider the remote machine dead, I chose 9 seconds.</p> <p>when the server receives a hello message it stores the timestamp in a list among with the ip address of the client.</p> <p>on the timer_elapsed event</p> <pre><code>void holdtimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { try { if (server.Clients.GetAllItems().Count != 0) { foreach (IScsServerClient _client in server.Clients.GetAllItems()) { string ipaddr = stripip(_client.RemoteEndPoint.ToString()); bool containsentry = _clientlist.Any(item =&gt; item.ipadress == ipaddr); if (containsentry) { foreach (Scsserverclient entry in _clientlist.Where(entry =&gt; entry.ipadress == ipaddr)) { if ((DateTime.Now - TimeSpan.FromSeconds(Settings.Default.Hold)) &gt; entry.lasthello &amp;&amp; entry.isConnect != "Disconnected") { entry.client.Disconnect(); } } } } } } catch (Exception ex) { } } </code></pre> <p>basically i run through a list of connected clients and if the last received hello message is older than 9 seconds, i consider to client to disconnected.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload