Roxana
Roxana

Reputation: 47

TCP server with multiple clients C code

I have written a TCP server application in C code that can handle multiple clients connections at the same time. When the server receive data from one client, all the clients should receive it. I've used select() in order to create the connection between server and multiple clients but i don't know how to do that all the clients receive the same data at the same time and each of them be able to send data to the server.

read_option(fd) is my function used in the application

while(1)
{
    select (nfds+1, &readfds, NULL, NULL, &tv);
    if (FD_ISSET (sd, &readfds))
    {
        len = sizeof (from);
        bzero (&from, sizeof (from));
        client = accept (sd, (struct sockaddr *) &from, &len);
        if (client < 0)
        {
            continue;
        }
        if (nfds < client) 
            nfds = client;
        FD_SET (client, &actfds);
        fflush (stdout);
    }
    for (fd = 0; fd <= nfds; fd++)  
    {
        if (fd != sd && FD_ISSET (fd, &readfds))
        {
            if (read_option(fd))
            {
                fflush (stdout);
                close (fd);
                FD_CLR (fd, &actfds);
            }
        }
    }

Upvotes: 1

Views: 1974

Answers (1)

Mats Petersson
Mats Petersson

Reputation: 129514

If you want to send and receive from multiple clients at the same time - at least for the receive side - you will need to use threads, as there is no way all your clients can send data to your server at once, and each client's packets will need to be handled separately. (I'm assuming that the data received in "read_option" is more than a few bytes and takes more than a few microseconds to process - if that assumption is false, then you may be able to do what you are currently doing - but I'm pretty sure it's EASIER to solve it using threads). Obviously, also, if you have sufficient number of clients, you may still not have enough CPU or network bandwidth to process all packets within a set amount of time.

It may be possible to use multicast to send to all clients simultaneously - but you can't guarantee that all clients receive the data simultaneously, - certainly not if we are talking computer time simultaneously. If the clients are on a the same network as the server, and if we are talking human reaction time (0.05-0.1s), then perhaps you can achieve that. If the machines are distributed over the entire internet, you should be happy to achieve 0.1-0.5s - and quite possibly worse.

So, given the comments:

Because you are doing send() and receive() in read_option(), your read_option will block at that point, so any other client will not be processed.

You will essentially need to start a thread for each client, using pthread_create(). You can then "chat" between the server and each of the clients independently of each other. I expect you'll ALSO need to have some sort of synchronization between each thread, so that they don't run ahead of each other, or some such. Since I don't know what game you are playing, I'm not sure what the "rules of play" should be, and can't really comment on that - in fact, I think that's a great subject for another question, rather than in this question [otherwise, I'll fear it never ends!]

Upvotes: 1

Related Questions