I've been playing around with SDL_Net, trying to get a simple client-server communication model working. Some old threads I've found on this forum have been really helpful to me :)
Currently I've managed to get one server listening for input from multiple clients, however when I try to send this input back to the clients (ie. a simple chat system) the clients that receive this input display nonsense.
I know the problem is probably something really stupid and basic, but I can't for the life of me figure out what it is.
Here's what it looks like on the client window:
Here's what it looks like on the server window:
Here's the relevant server code:
...vector<char *> messagesC;...void handleNetworkData(Clients &clients, SDLNet_SocketSet socketSet){ if(!socketSet){ return; } const int timeout = 0; int active = SDLNet_CheckSockets(socketSet, timeout); // If there is activity. if(active > 0){ for(int i = 0; i < clients.size(); i++){ if(TCPsocket client = clients.at(i).getCSD()){ // Check if socket has had some activity if(SDLNet_SocketReady(client)){ clients.at(i).setLastActiveTick(SDL_GetTicks()); const int bufferSize = 511; // +1 to ensure there is room for NULL terminator char buffer[bufferSize + 1]; // Read the data. int bytes = SDLNet_TCP_Recv(client, buffer, bufferSize); switch(bytes){ case -1: // error handling break; case 0: // Socket was closed by remote host. // Close the socket and null the pointer in the vector. printf("Host %d disconnected\n\n", i); SDLNet_TCP_DelSocket(socketSet, client); SDLNet_TCP_Close(client); clients.erase(clients.begin()+i); break; default: // We got some data! // Ensure it is NULL terminated and print. buffer[bytes] = '\0'; printf("\nClient %d says: %s\n\n", i, buffer); messagesC.push_back(buffer); break; } } } } }}void sendMessages(Clients &clients, SDLNet_SocketSet socketSet){ for(int i=0; i<messagesC.size(); i++){ char *theMessage = messagesC.at(i); int length = sizeof(messagesC.at(i)); for(int j=0; j<clients.size(); j++){ if (SDLNet_TCP_Send(clients.at(j).getCSD(), theMessage, length) < length){ printf("SDLNet_TCP_Send: %s\n", SDLNet_GetError()); //exit(EXIT_FAILURE); } else{ cout << "Message sent on socket " << clients.at(j).getCSD() << " - " << theMessage << ".\n"; } } } messagesC.clear();}...
Here's the relevant client code:
...void recievePackets(){ const int timeout = 0; int active = SDLNet_CheckSockets(socketSet, timeout); // If there is activity. if(active > 0){ if(TCPsocket server = sd){ if(SDLNet_SocketReady(sd)){ const int bufferSize = 511; // +1 to ensure there is room for NULL terminator char buffer[bufferSize + 1]; // Read the data. int bytes = SDLNet_TCP_Recv(sd, &buffer, sizeof(buffer)); //cout << buffer << endl; switch(bytes){ case -1: // error handling break; case 0: // Socket was closed by remote host. // Close the socket and null the pointer in the vector. cout << "Server %d disconnected\n\n"; SDLNet_TCP_DelSocket(socketSet, sd); SDLNet_TCP_Close(sd); break; default: // We got some data! // Ensure it is NULL terminated and print. buffer[bytes] = '\0'; printf("\nServer says: %s\n\n", buffer); break; } } } }}...int main(int argc, char **argv){... while (running){ sendPackets(); recievePackets(); }...}
Anybody know what is causing the problem?