1

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);
            }
        }
    }
1
  • 1
    You must consider something called multicasting, but you must be careful. If you don't like that, then you can simply implement something more naive, the server will send to every client whatever it receives. That implies, that you store your client IPs' on a map or an array. Commented Dec 28, 2012 at 12:12

1 Answer 1

1

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!]

Sign up to request clarification or add additional context in comments.

8 Comments

How do I use threads ? My application need only two clients (it's about a game and each player should see the table game and the moves of the other player.The game is implemented in that function "read_option")
For two players, assuming you are not playing chess or some such, I'd say just doing what you are doing right now is fine.
But if you want to use threads, look up "pthreads" for example.
In this moment only the first client who start the game is able to comunicate with the server (receive-send data and the app work "fine" because the first client can play as there are 2 players), while the second client doesn-t receive nothing but the main menu game and doesn-t see the actions that the first one have made.
Are you sure you want to close (fd); in the middle of your loop? Not saying that's why things are going wrong, but it could be... Beyond that, I think we need to see what read_option does.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.