Multiple Client Connections

This is a discussion on Multiple Client Connections within the Windows Programming forums, part of the Platform Specific Boards category; Hi, Up until now i have only needed my server to handle one client connection at a time. But i ...

  1. #1
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339

    Multiple Client Connections

    Hi,

    Up until now i have only needed my server to handle one client connection at a time. But i am now working on a bigish chat server program and i am unsure how to have more that one client connected at a time.

    Is it somthing to do with the number of backlog connections?
    At the moment i am using:

    #define BACKLOG 1

    So i assume if i did

    #define BACKLOG 6

    Then i could have 6 connections waiting?, Although i woudnt want them waiting, i need them all connected at once.

    Also how would i handle each connection individually? Say for instance on the chat server there were 4 people connected. How would i then get say the 3rd persons IP so he could be disconneted.

    Because at the moment i am handling all my connections on 1 socket:

    sock2 = accept(sock1,(struct sockaddr *)&client,&sin_size);

    And sock1 is listing for connections.

    I have tried to make that clear, but basically i need to adjust my code from taking and clearly handling 1 connetion from a client to taking up to say 15 potential client connections.

    Thanks for any help,
    TNT
    Last edited by (TNT); 04-05-2002 at 06:28 AM.
    TNT
    You Can Stop Me, But You Cant Stop Us All

  2. #2
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    A good start would be to create a client class with the socket and pointers to two other clients that could be used to set up a kind of linked list. At this point there are two ways for the server to handle concurrent connections. You could either spawn a new thread for each client (which shouldn't be too taxing on the server since your user limit is fifteen), or have a master thread iterate through the client sockets, seeing if there are any messages to intercept. If you aren't utilizing them already, async sockets are especially convenient since you can have them automatically send a message to you if incoming data is present.

    All that would remain is matching the socket sending the message to the current iterator socket, and dealing with the situation as needed. If a user causes some global event (like saying something, disconnecting, or connecting), just loop through the list and send notifications to everybody.

    btw, listen() allows you to specify a backlog value as the last parameter. Passing it SOMAXCONN allows the "underlying service provider" to determine the length of the waiting-connection queue.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple forks in one client connection (ftpclient)
    By Dynamo in forum Networking/Device Communication
    Replies: 5
    Last Post: 01-16-2011, 12:41 PM
  2. Testing maximum client connections
    By abachler in forum Networking/Device Communication
    Replies: 6
    Last Post: 05-02-2008, 09:31 AM
  3. Multiple Connections to a Server
    By niqo in forum Networking/Device Communication
    Replies: 9
    Last Post: 04-03-2008, 10:36 AM
  4. Best programming practices for multiple connections
    By vasillalov in forum Networking/Device Communication
    Replies: 8
    Last Post: 10-09-2007, 11:14 PM
  5. multiple connections via sockets
    By cope in forum Networking/Device Communication
    Replies: 7
    Last Post: 06-07-2007, 11:35 AM

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