Imagination is more valuable than knowledge - Albert Einstein
0 Members and 1 Guest are viewing this topic.
WaitForSingleObject(hMutex, INFINITE); // Lock the mutex FD_SET TempSet = masterSet; // Make a temp location so we can unlock our mutex and let our accept thread run ASAP ReleaseMutex(hMutex); // Unlock the mutex if (TempSet.fd_count == 0) // If a set is empty and we call form it, it returns a error, thats too much coding for me { // So we will simply check if there is anything in it, if so we will do something, else screw it -.-; continue; } // select() has 5 member datas of use to us they are // - the number of file descriptors // - a FD_SET checks readability // - a FD_SET checks for writeability // - a FD_SET checks for errors // - a wait time timeval waitTime; // Set up our interval waitTime.tv_sec = 0; // Set it to 0 waitTime.tv_usec = 0; // Set it to 0 int result = select(TempSet.fd_count, &TempSet, NULL, NULL, &waitTime); // Select a socket in out TempSet if (result == 0) { continue; } if (result == SOCKET_ERROR) { cout << "Server: Sorry Sir! there was a error in the Select() macro!\n"; continue; } cout << "Type: "; gets(outMessage); for (unsigned int i = 0; i < TempSet.fd_count; i++) { DWORD clientSocket = TempSet.fd_array[i]; DWORD messageSize = strlen(outMessage); messageSize = htonl(messageSize); if ((nBytes = send(clientSocket, (char*)&messageSize, sizeof(messageSize), 0)) == SOCKET_ERROR) { Log("send() failed"); } messageSize = ntohl(messageSize); if ((nBytes = send(clientSocket, outMessage, messageSize, 0)) == SOCKET_ERROR) { Log("send() failed"); } //Bunch of stuff }
I'm unable to send() to multiple clients. I have a different thread accepting clients, I can see when they join/leave, and stuff, but I'm having a problem sending messages/commands. Here's what happens:>I open server>Open client>Friend opens another client>I can send messages and commands to MYSELF, but not the friend. If my friend opened the client first, I can send messages and commands to him, but not myself. Code: [Select]WaitForSingleObject(hMutex, INFINITE); // Lock the mutex FD_SET TempSet = masterSet; // Make a temp location so we can unlock our mutex and let our accept thread run ASAP ReleaseMutex(hMutex); // Unlock the mutex if (TempSet.fd_count == 0) // If a set is empty and we call form it, it returns a error, thats too much coding for me { // So we will simply check if there is anything in it, if so we will do something, else screw it -.-; continue; } // select() has 5 member datas of use to us they are // - the number of file descriptors // - a FD_SET checks readability // - a FD_SET checks for writeability // - a FD_SET checks for errors // - a wait time timeval waitTime; // Set up our interval waitTime.tv_sec = 0; // Set it to 0 waitTime.tv_usec = 0; // Set it to 0 int result = select(TempSet.fd_count, &TempSet, NULL, NULL, &waitTime); // Select a socket in out TempSet if (result == 0) { continue; } if (result == SOCKET_ERROR) { cout << "Server: Sorry Sir! there was a error in the Select() macro!\n"; continue; } cout << "Type: "; gets(outMessage); for (unsigned int i = 0; i < TempSet.fd_count; i++) { DWORD clientSocket = TempSet.fd_array[i]; DWORD messageSize = strlen(outMessage); messageSize = htonl(messageSize); if ((nBytes = send(clientSocket, (char*)&messageSize, sizeof(messageSize), 0)) == SOCKET_ERROR) { Log("send() failed"); } messageSize = ntohl(messageSize); if ((nBytes = send(clientSocket, outMessage, messageSize, 0)) == SOCKET_ERROR) { Log("send() failed"); } //Bunch of stuff }The client basically just recv()s and checks for certain commands (ex: "/CloseSocket") and displays the message if it doesn't match any of the commands. I just copy-pasted the code from one of celestialkey's tutorial and put them into my own executable and .dll. Also, select() takes a long time to respond. Is there a way to shorten how long it waits by altering waitTime?Tell me if you guys need more of the code, as I assume the problem is somewhere here.[I barely understand sockets. I suck at C++. Bare with me.]
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
I'd prefer to die standing, than to live on my knees - Che Guevara
If you change the way you look at something, does that something change in any way? - Quantum Theory
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.
It takes skill to build an empire. It takes an idiot to maintain it.
A bit off-topic but if you wish to support concurrency and you have no experience using winsock is an horrible idea. I would go for a library like Boost Asio or POCO, they both are very efficient and will take care of the concurrency behind a worker thread system. They don't enforce any premade protocol on you either, only give you an equivalent read / write system. This will most likely save you a lots of time and you won't run into errors like this one, and judging by your current code which is not using the most efficient way to handle networking on windows, you will gain performance, and also cross-platform ability.http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio.htmlhttp://pocoproject.org/