Andna
Andna

Reputation: 6689

Very simple server in C

As one of the assigments we have to implement very simple server in C with some clients. The idea is that using system V IPC queues we create one queue where clients register and then for every client there is one queue with messages. I am wondering a bit about server part. Should I have something like this:

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

so for every time interval I check every queue and do what I have to do, or maybe I should use signals to inform server that at least one of the queues is ready to be managed.

How is it done in normal servers, do they have some time interval after which they check everything they need to do or there is more proper way to do this?

Upvotes: 5

Views: 596

Answers (1)

twitch
twitch

Reputation: 183

You have to do something along the lines of this:

This is a very basic answer, and needs definitions and prototypes to be in place, however that should give you a basic select, example.

this code works both on freebsd, ubuntu, and my windows computer (Given you have the correct headers) Also its been minified and some definitions removed, suck as socket descriptor defs because those are pretty much - it is what it is.

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.

Select is portable to Win32 as well as UNIX, though it isn't the recommended choice if you are doing any heavy lifting on sockets in unix IE: FreeBSD. use kqueue or epoll or the like if you need to get deeper and have greater management of your sockets.

Upvotes: 3

Related Questions