More Winsock threads: Proper way to recv() and send() to multiple clients

This is a discussion on More Winsock threads: Proper way to recv() and send() to multiple clients within the Windows Programming forums, part of the Platform Specific Boards category; Hi, Im coding a chat like program (async stream sockets). I have a server and there can be multiple clients. ...

  1. #1
    Unregistered
    Guest

    More Winsock threads: Proper way to recv() and send() to multiple clients

    Hi, Im coding a chat like program (async stream sockets).
    I have a server and there can be multiple clients. When a client writes something, the message is sent to the server, it receives it and then send it to the other clients.

    What Im doing to send it to the other clients is (with proper error checking):

    Code:
    for (i = 0; i < NumOfClients; i++) {
      
       send(server.ClientSockets[i], t,strlen(t),NULL);
    
    }
    This seems right to me, what I don't know is how to receive data when having multiple clients. Im doing something like this, tell me if its the right way, or if there is a better one.

    Code:
    case FD_READ:
    
    for ( int i = 0; i < server.NumOfClients; i++) {
    
      int ret = recv(server.ClientSockets[i], b, sizeof(b),0);
    
      if (ret == SOCKET_ERROR) {
         // do nothing
      } else {
    
        string tempstring = b;
        chatbuffer.push_back(tempstring);
      }
    
      // broadcast to other clients
      memset(b,0,1024);
    
    }
    Thx in advance!

  2. #2
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    Does that particular method work?
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  3. #3
    Unregistered
    Guest
    Yeah, but I have to do extra error checking...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. send and recv are cpu intensive?
    By Rune Hunter in forum Networking/Device Communication
    Replies: 14
    Last Post: 09-04-2007, 10:24 AM
  2. send() and recv() functions
    By kris.c in forum Networking/Device Communication
    Replies: 9
    Last Post: 06-24-2006, 10:41 PM
  3. send and recv
    By DeadManWalking in forum C Programming
    Replies: 1
    Last Post: 12-04-2005, 09:17 AM
  4. recv multiple lines
    By TCM in forum Networking/Device Communication
    Replies: 11
    Last Post: 07-13-2004, 05:54 PM
  5. Using recv() and send()
    By Sam_bham in forum C Programming
    Replies: 3
    Last Post: 06-08-2004, 05:36 PM

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