TCP Sockets: multiple clients - one server

This is a discussion on TCP Sockets: multiple clients - one server within the C Programming forums, part of the General Programming Boards category; Hi all, I am trying to create an application that uses TCP sockets under Unix, where multiple clients connect to ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    2

    TCP Sockets: multiple clients - one server

    Hi all,

    I am trying to create an application that uses TCP sockets under Unix, where multiple clients connect to one server. Each message sent by one client is forwarded to all other clients. I am using on the server side an array of int's declared at the beginning of the application, that will contain the socket descriptor returned by the "accept" function for each client that connects. After this, I am fork-ing a new child that will handle that client. All child processes need to access the array of int's in order to send to everyone the client messages.

    My problem is that for the first client (child process), the array of int's has only one element, namely its own socket (file) descriptor, for the second client, the array of int's has two elements, like it is supposed to have, but the first child process created for the first client still has only one element, instead of two. I have no idea why and how to change the code so that the first client will also have 2 elements when the second client connects to the server. I have tried declaring the array as static, using pointers, etc, but nothing works.

    Server code snippet:

    Code:
    ...
    int clients[5], i = 0;
    //int * clients = (int *)calloc(5, sizeof(int));
    int sd;
    ... 
    socket(...);
    bind(...);
    listen(...);
    ...
    while (1) {
       sd = accept(...);
       clients[i] = sd;
       i++;
       ...
       if (fork() == 0) {
          ...
          for(i = 0; i < 5; i++) {
             send(clients(i), ...);
          }
          ...
       }
       ...
    }
    Any help is appreciated, thank you!

  2. #2
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    Hmm good question. I'll have a little play with fork(), seems like it should work. If you're not bothered about which method to use, you should use select() to do this, the link even provides an example of what you're trying to accomplish.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    2
    I am currently restructuring the server side so that it uses select() instead of fork()-ing processes. The problem is that the child processes copy everything from parent, so global variables are not shared, but copied instead. I tried with static variable but no luck there either.

  4. #4
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    There must be some way. I haven't managed to do it, the attached code shows my attempt. Useless. Hopefully, someone with more experience will enlighten us.
    Attached Files Attached Files

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    The approach is just not right. As you see, you can't just fork() when each new client connection comes in, because the new process is not aware of connections that will be made later on. You can't do this with separate processes like this.

    You need to either use threads, or a multiple-resource wait method like select().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple clients to "internet" server
    By Zarniwoop in forum C Programming
    Replies: 2
    Last Post: 10-12-2008, 12:04 AM
  2. MISO Soup: Multiple Clients and one Server
    By doubleanti in forum Networking/Device Communication
    Replies: 2
    Last Post: 07-24-2007, 03:29 AM
  3. Problem using sockets for multiple clients
    By turmoil in forum Networking/Device Communication
    Replies: 3
    Last Post: 04-01-2005, 07:12 PM
  4. Server with multiple clients
    By Thantos in forum Networking/Device Communication
    Replies: 20
    Last Post: 09-02-2003, 06:52 PM
  5. server with multiple clients
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-23-2002, 09:15 PM

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