Thread: Server Program Advice

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    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!
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    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?
    Last edited by pobri19; 03-21-2009 at 04:46 AM.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    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.

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    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)?
    Last edited by pobri19; 03-22-2009 at 03:32 AM.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

Popular pages Recent additions subscribe to a feed

Similar Threads

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