1

I have a server that needs to gather info from its clients. The info is a random number that I store into an array of frequency. The server should tell all clients the most frequent number, but every 30 sec or so. The clients that sent that number get disconnected and make space for new ones. So, new numbers are added in the array.

How do I make the server make the check every 30 sec and send the message?

I work in C under Unix, not C++.

  • try sleep() function.. http://stackoverflow.com/a/13385922/2648826 – Digital_Reality Jan 16 '14 at 08:38
  • "every 30s" and "after 30s" are two different statements. Try to study the [crontab](http://en.wikipedia.org/wiki/Cron) – ShinTakezou Jan 16 '14 at 08:40
  • Yeah, I meant every 30s. It should do the check every 30 sec and update the answers.sleep will do the check once, then stop. I need it to do it every 30 sec. permanently as while the server runs –  Jan 16 '14 at 08:41
  • a cron job then is what you need IMO. – ShinTakezou Jan 16 '14 at 08:45
  • You could use sleep and just put a loop around what you want to repeat, but crontab would be better. – Phil_12d3 Jan 16 '14 at 08:49
  • It seems that crontab executes commands and programs, while I need a piece of code to do the check every x sec, not the entire program. –  Jan 16 '14 at 08:51
  • You ll at least need a thread for this, and Phil_12d3 already said what you should put in it. But again, sleep do not garantee that the code will run X sec later, but _at least_ X sec later, while cronjob are more precise. – DrakaSAN Jan 16 '14 at 09:14
  • Cron use minute not second resolution. How do you configure cron for 30 second interval? – vmario Jan 16 '14 at 11:18
  • @vmario you are very right. In that case, a code solution is the only option I can think of. http://stackoverflow.com/questions/8670328/cron-running-cron-every-1-second – Phil_12d3 Jan 16 '14 at 13:04

4 Answers4

2

Use alarm()

 void handler(int signum) {
    ///your logic
 alarm(30);
 }

 int main(void) {

  signal(SIGALRM, handler);
  alarm(30);

  getchar();

  return 0;
}
Rahul R Dhobi
  • 5,668
  • 1
  • 29
  • 38
0

Try using POSIX per-process timer: http://man7.org/linux/man-pages/man2/timer_create.2.html In the examples section there's an example invoking a timer every 100ns.

0

you can simply use these functions :

 int nanosleep(const struct timespec *req, struct timespec *rem); or 
 usleep(microseconds);

these functions make the program sleep for x seconds

exemple :

while(1) {

function(); //function to run each 30 sec 
 usleep(30*1000000); 
}
Anis_Stack
  • 3,306
  • 4
  • 30
  • 53
-2

Try the following pseudocode logic:

while (TRUE) { //this can be your main loop

   //get current time and if more than 30 seconds after last send
   {
       //get most frequent number

       //inform clients

       //get current time and store timestamp as last send
   }

}

nikpel7
  • 656
  • 4
  • 11
  • Hm, actually it's like this: server informs clients every 30 sec and then the array is set to 0.Clients have 30 sec to send their number to the server, but wait the reply which will happen the next cycle, where again, they get the reply and the array is set to 0 again. –  Jan 16 '14 at 09:17
  • hmm, the way you describe it, it seems to me that you need some synchronization mechanism between the client and the server, I mean other than the 30 seconds. For example what will happen if a message between the client is lost or gets delayed significantly? Will this mess with the timer of the server and the client? – nikpel7 Jan 16 '14 at 09:26
  • I don't care about those details now, just want to make it work. –  Jan 16 '14 at 09:30