tags:

views:

72

answers:

6

Hi!

I have written C code for a TCP server to listen to one client at a time.. but I'm having difficulting finguring out how I can get the server to listen to multiple clients at one time.

Does any one know of a good tutorial or example explaining this?

Thanks!

A: 

see man 2 select

This should do what you want. Beware, untested:

listen_any(int n, int *s, void receiver(int *))
{
    FD_SET set;
    int x = -1;
    int i;
    for (i = 0; i < n; ++i)
        if (x < s[i]) x= s[i];
    while (1) {
        FD_ZERO(&set);
        for (i = 0; i < n; ++i)
            FD_SET(s[i], fd);
        select(x, &set, NULL, NULL, NULL);
        for (i = 0; i < n; ++i)
            if (FD_ISSET(s[i], fd)) {
                struct socakddr sa;
                int len = sizeof(sa);
                int sn = accept(s[i], &sa, &len);
                if (sn >= 0) {
                    if (!receiver(sn))
                       close(sn);
                }
            }
    }
}
Joshua
A: 

Just create another listener when previous one was taken

pinichi
+2  A: 

There are a number of ways to achieve this, some of which are:

  • use select to basically wait on a group of file descriptors until one of them is ready for reading or writing.
  • use threading, such as pthreads, to hand off individual sessions to separate threads within the one process.
  • use fork to replicate the process for handling a session, so that the forked process handles that session and the original goes back to waiting for more connections.

Of these, I prefer the middle one. Forking a process is a sometimes-expensive operation in that the data normally has to be copied if either process attempts to change it.

The select option means that your code has to manage multiple sessions and that can sometimes get messy.

With threading, you can separate relatively easily the sessions from each other, without incurring the cost of process duplication. Of course, threading has its own pitfalls if you're not careful but I consider it a preferable option once you understand the potential problem areas.

paxdiablo
+1  A: 

Take a look at libevent. It powers several popular high-scalability server applications.

James Dunne
+1  A: 

What platform are you on? If it's Windows, a popular platform API is WaitForMultipleObjectsEx. If it's Linux, popular choices from lower- to higher-level might be select (as mentioned by Joshua), epoll, kqueue, or libevent/libev (similar but slightly different libraries which are among other things abstraction layers upon the preceding APIs).

select is quite portable, but isn't optimal in most cases (a lot has been learned since it was created). Still, if you don't need crazy performance and want to stick to pretty standard C programming, it would be a fine choice, and there is a ton of literature that refers to it. And once your server works with select, it shouldn't be too hard to change it to one of the more advanced multiplexing APIs.

John Zwinck
+1  A: 

Checkout http://www.zeromq.org/ It's a pretty cool networking(or messaging) library that takes 'select' or conventional sockets away from the code.

smailq