(Sorry for the no-info-title lol.)
Hi! I have a server, and a client program, bothcompile and connect to each other fine, I can connect multiple users and all the good stuff...
But when I try to send data to the server from one of the clients, I need to put 10 characters infront of the message I want to send, otherwise the data is corrupt.
Does anyone know the cause of this madness?
Client (Only posted main loop)
while (true)
{
//Connecting to server
if (!online)
{
cout << "Attempting to connect to server (" + ip + " " + std::to_string(port) + ")." << std::endl;
socket.connect(ip, port);
if (socket.getRemoteAddress() != sf::IpAddress::None) // If remote IP Address DOES NOT = NULL we are connected to server
{
online = true;
cout << "Connected to server successfully!" << std::endl;
}
else // Otherwise we are not connected
{
online = false;
cout << "Could not connect to server!" << std::endl;
}
}
getline(cin, input);
socket.send(input.c_str(), sizeof(input), bytes_sent);
while (bytes_sent != sizeof(input))
{
socket.send(std::to_string(input.at(bytes_sent)).c_str(), sizeof(input.c_str()), bytes_sent);
}
}
Server (only posted mian loop)
for (unsigned int i = 0; i < clients.size(); i++) //For loop that iterates though each client, receives data and deals with that data
{
clients.setBlocking(false);
// Check to see if client is still connected
if (clients.receive(buffer, 10, received) == sf::Socket::Disconnected)
{
std::cout << "User #" << i << " has discconected and has been removed from the user list" << std::endl;
clients.disconnect();
numClients--;
continue;
}
if (clients.receive(buffer, sizeof(buffer), received) != sf::Socket::Done) //Receives data (and checks if data was fully received)
{
}
else
{
if (received > 0) //Checks if data was received.
{
std::cout << "Received \"" << buffer << "\" From IP " << clients.getRemoteAddress().getPublicAddress() << std::endl; //Prints received string
//clients.send(serverState.c_str(), serverState.length(), bytes_sent);
//while (bytes_sent != sizeof(serverState))
//{
// clients.send(serverState.c_str(), sizeof(serverState.c_str()), bytes_sent);
//}
}
}
}
All information will be useful