views:

62

answers:

1

In C, when making a networking client / server setup, I usually have to do some standard BSD socket setup. Then on the server side, I'll have to manage multiple threads, usually a main thread, an a io thread. Each connection is managed by a connection manager so that you can have connections being processed while new requests are coming in.

What are some good ways to do connection management in C? Are there well know libraries to handle all of this? I know about Boost for C++, but I'm interested in C and Python.

Thanks, Chenz

P.S. Sorry about the not so thought out question. I'll try and polish it up soon.

+2  A: 

Personally, I am not a huge fan of the one-thread-per-connection model with synchronous IO. I prefer X threads with a pool of Y connections with asynchronous IO. You can spawn threads as needed, or round robin the connections as they come in to a pre-allocated pool.

If you want to be really tricky, spawn threads with lifetime management, where new connections go to the newest spawned thread so the old thread can be killed off. That way if a thread holds on to a resource, when it is cleaned up the resource will be released.

You may want to look at select, poll, epoll, completion pools and AIO.

Most of these are wrapped up in libevent.

taspeotis
I understand one-thread-per-connection is bad. My IO thread would handle all IO. I do like your lifetime idea though. Select, poll, and epoll always seem to add a complexity to code that I want handled in a maintained library.
Crazy Chenz