recieve data, then send data to all other clients
i have just slightly modified the code from gametutorials.com on allowing multiple connections to a server. what i tried to do with this code is, when a client sends a message to the server the server sends that message to all other clients connected to the server. I thought that this code would work:
Code:
#pragma comment(lib, "wsock32.lib")
#include <winsock.h>
#include <windows.h>
int startupServerForListening(unsigned short port);
void shutdownServer(int socket);
HANDLE threadHandle;
HANDLE mutexHandle;
FD_SET allsockets;
bool gQuitFlag = false;
void acceptingThreadProcedure(int* serverSocket) {
int mySocket = *serverSocket;
for (;;) {
unsigned int clientSocket = accept(mySocket, 0, 0);
if (clientSocket == SOCKET_ERROR) {
MessageBox(0,"Accept Failed!","",0);
gQuitFlag = true;
return;
} else {
WaitForSingleObject(mutexHandle, INFINITE);
FD_SET(clientSocket, &allsockets);
ReleaseMutex(mutexHandle);
MessageBox(0,"Client Connected","",0);
}
}
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow){
int serverSocket;
serverSocket = startupServerForListening(7654);
if (serverSocket == -1) {
MessageBox(0,"ERROR","",0);
return 0;
}
mutexHandle = CreateMutex(NULL, false, NULL);
if (mutexHandle == NULL) {
MessageBox(0,"Error creating mutex","",0);
shutdownServer(serverSocket);
return 0;
}
int threadId;
threadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)acceptingThreadProcedure, &serverSocket, 0, (LPDWORD)&threadId);
if (threadHandle == NULL) {
MessageBox(0,"ERROR STARTING LISTENING THREAD","",0);
shutdownServer(serverSocket);
return 0;
}
Sleep(1000);
FD_ZERO(&allsockets);
for (;;) {
if (gQuitFlag) {
break;
}
Sleep(400);
WaitForSingleObject(mutexHandle, INFINITE);
FD_SET tmpallsockets = allsockets;
ReleaseMutex(mutexHandle);
if (tmpallsockets.fd_count == 0) {
continue;
}
timeval waitTime;
waitTime.tv_sec = 0;
waitTime.tv_usec = 0;
int result = select(tmpallsockets.fd_count, &tmpallsockets, NULL, NULL, &waitTime);
if (result == 0) {
continue;
}
if (result == SOCKET_ERROR) {
MessageBox(0,"ERROR IN SELECT", "",0);
continue;
}
for (unsigned int i = 0; i < tmpallsockets.fd_count; i++) {
unsigned int clientSocket = tmpallsockets.fd_array[i];
int nBytes;
#define MAX_MESSAGE_SIZE 4096
char buffer[MAX_MESSAGE_SIZE];
nBytes = recv(clientSocket, buffer, sizeof(buffer), 0);
if (nBytes == SOCKET_ERROR) {
int error = WSAGetLastError();
if (error == WSAECONNRESET) {
WaitForSingleObject(mutexHandle, INFINITE);
FD_CLR(clientSocket, &allsockets);
ReleaseMutex(mutexHandle);
closesocket(clientSocket);
MessageBox(0,"A client disconnected","",0);
continue;
} else {
MessageBox(0,"SEND FAILED, SERVER TERMINATING","",0);
gQuitFlag = true;
break;
}
}
if (nBytes == 0) {
WaitForSingleObject(mutexHandle, INFINITE);
FD_CLR(clientSocket, &allsockets);
ReleaseMutex(mutexHandle);
closesocket(clientSocket);
MessageBox(0,"A CLIENT DISCONNECTED","",0);
continue;
}
buffer[sizeof(buffer)] = '\0';
for (i=0; i < tmpallsockets.fd_count; i++) {
unsigned int clientSocketout = tmpallsockets.fd_array[i];
if(clientSocket == SOCKET_ERROR){
MessageBox(0,"error","",0);
}
nBytes = send(clientSocketout, buffer, sizeof(buffer), 0);
MessageBox(0,"message sent","",0);
if (nBytes == SOCKET_ERROR) {
int error = WSAGetLastError();
if (error == WSAECONNRESET) {
WaitForSingleObject(mutexHandle, INFINITE);
FD_CLR(clientSocket, &allsockets);
ReleaseMutex(mutexHandle);
closesocket(clientSocket);
MessageBox(0,"A client disconnected","",0);
continue;
} else {
MessageBox(0,"SEND FAILED, SERVER TERMINATING","",0);
gQuitFlag = true;
break;
}
}
if (nBytes == 0) {
WaitForSingleObject(mutexHandle, INFINITE);
FD_CLR(clientSocket, &allsockets);
ReleaseMutex(mutexHandle);
closesocket(clientSocket);
MessageBox(0,"A CLIENT DISCONNECTED","",0);
continue;
}
}
}
}
shutdownServer(serverSocket);
return 0;
}
int startupServerForListening(unsigned short port) {
int error;
WSAData wsaData;
if ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) == SOCKET_ERROR) {
return -1;
}
int mySocket = socket(AF_INET, SOCK_STREAM, 0);
if (mySocket == SOCKET_ERROR) {
return -1;
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = INADDR_ANY;
if (bind(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) {
closesocket(mySocket);
return -1;
}
if (listen(mySocket, 5) == SOCKET_ERROR) {
closesocket(mySocket);
return -1;
}
return mySocket;
}
void shutdownServer(int socket) {
WaitForSingleObject(threadHandle, INFINITE);
CloseHandle(threadHandle);
CloseHandle(mutexHandle);
closesocket(socket);
WSACleanup();
}
but it doesn't, it recieves the message from a client then sends that same message back to the client that sent it, but to nobody else, just the client that sent it. and i really don't know why it's doing it.
Could you please tell me why it's doing what it is?