C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 03-21-2009, 01:22 AM   #1
Registered User
 
Join Date: May 2008
Location: Australia
Posts: 198
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?

Thanks!
__________________
Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
pobri19 is offline   Reply With Quote
Old 03-21-2009, 01:34 AM   #2
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,336
Quote:
Originally Posted by pobri19 View Post
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?

Thanks!
with your approach the problem will be not number of sockets - but number of threads, that could effectively perform at the same time...

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
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-21-2009, 01:39 AM   #3
Registered User
 
Join Date: May 2008
Location: Australia
Posts: 198
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?
__________________
Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

Last edited by pobri19; 03-21-2009 at 04:46 AM.
pobri19 is offline   Reply With Quote
Old 03-21-2009, 08:33 AM   #4
Malum in se
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 3,188
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.
__________________
Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.
abachler is offline   Reply With Quote
Old 03-22-2009, 12:17 AM   #5
Registered User
 
Join Date: May 2008
Location: Australia
Posts: 198
Quote:
Originally Posted by abachler View Post
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.

Maybe a parallel vector of threads and sockets (ints)?
__________________
Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

Last edited by pobri19; 03-22-2009 at 03:32 AM.
pobri19 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
telnet server, how to program a backspace Mastermosley C# Programming 5 03-22-2009 02:14 AM
Advice on writing a basic encryption program? osiris^ C Programming 2 09-10-2007 02:02 PM
Using variables in system() Afro C Programming 8 07-03-2007 12:27 PM
Function in a Server program nick048 C Programming 1 03-31-2007 01:41 AM
First Program need advice A10 C++ Programming 14 11-29-2006 11:00 PM


All times are GMT -6. The time now is 05:39 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22