Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Winsock: specified sending (http://www.programmingforums.org/showthread.php?t=14696)

jayme Dec 5th, 2007 1:32 PM

Winsock: specified sending
 
I'm having trouble finding the error here. When I have multiple users connected to the server, if one user sends a message, the server will end up sending it back to all users(including the user who sent the original message). What I need to do is allow the server to send to all users minus the original sender. Here's what my code looks like, the commented area is where I'm assuming the code needs some fixing, but I've tried a bunch of different combinations of removing the original sender from the sending sockets, but then the server only sends to certain users.

:

if ((bytes = recv(i, recvbuf, sizeof(recvbuf), 0)) <= 0)
                {
                    for(int h = 0; h < my_vec.size(); h++)
                    {
                        tempuser.id = i;
                        tempuser2 = my_vec.at(h);
                        if(tempuser2.id == tempuser.id)
                        {
                                my_vec.erase(my_vec.begin()+h);
                        }
                    }
                    std::cout << tempuser2.name << " has left the server. Socket "
                    << i << " is now free." << std::endl;
                    closesocket(i);
                    FD_CLR(i, &master);
                } else {
                    for(j = 0; j <= fdmax; j++)
                    {
                        if (FD_ISSET(j, &master))
                        {
                            { 
                                if(j == i)break;    // if the sending socket is equal to the socket receiving the message, exit loop.
                                send(j, recvbuf, sizeof(recvbuf), 0);
                            }
                        }
                    }
                }


The Dark Dec 5th, 2007 6:15 PM

Re: Winsock: specified sending
 
I don't think you want to exit the loop when you hit "i" - that would only send the message to the portion of the users whose socket was less than the sender.
I think something like this should work in the inner if statement:
:

  1. ...
  2. {
  3.   // if the sending socket is equal to the socket receiving the message, don't send.
  4.   if(j != i)
  5.     send(j, recvbuf, bytes, 0);
  6. }
  7. ...


jayme Dec 5th, 2007 6:24 PM

Re: Winsock: specified sending
 
Wow, how did I miss that? Eh, what's done is done.

Thanks.


All times are GMT -5. The time now is 3:36 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC