Server Program Advice
Hi there, so here is the dilemma: Basically I'm creating a server listener program to run on a server. I want this application to be able to accept an unknown number of connections at any one time, and I want the server application to spawn a new thread for communication with each of these connections.
So, I have a few options (I think), I'm wondering what would be the best way to accomplish this task:
Assign a large static array of sockets and threads and hope the connections don't go over?
Use a dynamic data structure for the array of sockets and threads? If so, what data structure?
Another thing: Is there a limit on the amount of sockets for a machine running Windows and a machine running a Unix based distro? Oh and is there a limit for the amount of threads running at any one time?
with your approach the problem will be not number of sockets - but number of threads, that could effectively perform at the same time...
Originally Posted by pobri19
think about using selct approach which will processseveral connections. If you want to split work among several CPUs - build a thread pool, select number of threads based on number of CPUs available, and assign new connection to the thread from the pool wich has least number of active connections.
the datastructure for storing sockets info will depend on how ofter you need to search for individual members, and what will be the key for the search
Well, each connection will only be made for a short period (anywhere from around 1-10 seconds), and I'm expecting about 1 connection request ever 5-60 seconds. There will probably only be 1 CPU running on this server though, unless I can manage to afford a server with more CPUs (which I doubt - therefore it's probably best just to program it for 1).
Edit: Oh and an index of some sort would work fine for a key. So... Any suggestions? :D
Well, per thread works best if you dynamicalyl allocate teh socket object, then pass a pointer to that object to the thread on creation. So you have a listening thread which spawns the actual communication threads. Be aware though that thread per connection will have an effective limit of around 100 connections, after that you start spending too much time switching between threads. A socket pool is a better approach at that point, and spawn only as many communication threads as you have processors, or perhaps one less if you have several. This way the listener thread can just pu tnew connections into the pool, while the communication threads service each connection n turn.
Ok then I'll do that... But as I said before, I'm still uncertain what data structure I should be using :P That's my main concern at the moment.
Originally Posted by abachler
Maybe a parallel vector of threads and sockets (ints)?