Very simple server in C

As one of the solutions, we must implement a very simple C server with some clients. The idea is that using the V IPC system queues, we create one queue in which clients are registered, and then for each client there is one queue with messages. I'm curious about the server part. Should I have something like this:

while(1)
{
  //some queue using code
  sleep(100);
}

therefore, for each time interval, I check each queue and do what I need to do, or maybe I should use signals to tell the server that at least one of the queues is ready for management.

How is this done on regular servers, do they have a certain time interval after which they check everything they need to do, or is there a more correct way to do this?

+5
source share
1 answer

You need to do something like this:

This is a very simple answer and requires definitions and prototypes, however this should give you an example of a basic choice.

this code works both on freebsd, ubuntu, and on my windows computer (given that you have the correct headers). It has also been reduced and some definitions removed, sucking as a socket descriptor, because they are pretty much what they are.

struct timeval timeout;
int rc
fd_set wfdset,rfdset,errfdset;
//Do some checks put them in either read fdset or write fdset or error fdset 
FD_SET (socket_sd, &rfdset);
timeout.tv_sec = 0;
timeout.tv_usec = 250 * 1000;
rc = select (maxfds + 1, &rfdset, &wfdset, NULL, &timeout);
//loop through the sockets and read from them at this point.

The choice is ported to Win32 as well as to UNIX, although this is not recommended if you are doing heavy socket work on unix IE: FreeBSD. use kqueue or epoll or the like if you need to manage your sockets deeper and more efficiently.

+3
source

All Articles