[RESOLVED] Winsock recv split data
Posted: Sun Feb 19, 2017 12:01 am
Hey guys, this is a super strange question because I've never experienced this before.
So basically what's happening is in my main loop, I'm calling Server::getNewConnection() which checks for a new, you guessed it, connection. If a new client joins, a thread is created and ran.
My Server::getMessage() uses recv() to wait for data, it is blocking.
But for some reason, the data I'm getting is being split twice and inaccurately.
I am sending a struct like so:
And similarly receiving a message like:
After using recv, my Server::ClientThread gets a message twice in a row (loops twice, waits for data, loops twice, waits for data, ...)
but the information I receive is like this:
When there should only be 1 recv, not two, that looks like this:
I feel like this instance is abnormal and I've never crossed it before, just wanted to see if anyone had some suggestions since I'd been trying to figure out why it's doing this for the past 2 hours.
Code: Select all
void Server::ClientThread( int myID ) {
printf("ClientThread\n");
bool running = true;
while (running) {
Server::clients[myID]->getMessage();
if (myID == 0)
Server::clients[myID]->sendMessage("You are client #1");
else
Server::clients[myID]->sendMessage("You are not client #1");
Server::clients[myID]->sendMessages();
}
}
bool Server::getNewConnection() {
int sin_size = sizeof(struct sockaddr_in);
socklen_t temp=sin_size;
sockaddr clients_addr;
fd_set readSet;
FD_ZERO(&readSet);
FD_SET(server_skt, &readSet);
timeval timeout;
timeout.tv_sec = 0; // Zero timeout (poll)
timeout.tv_usec = 0;
if(select(server_skt, &readSet, NULL, NULL, &timeout) == 1) {
if ( (client_skt = accept(server_skt, &clients_addr, &temp))!=-1) {
Server::clients.push_back(new Client(client_skt));
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)Server::ClientThread,(PVOID)Server::clients.size()-1, NULL, NULL);
return true;
}
}
return false;
}
My Server::getMessage() uses recv() to wait for data, it is blocking.
But for some reason, the data I'm getting is being split twice and inaccurately.
I am sending a struct like so:
Code: Select all
struct dataStruct {
char message[SERVER_MAXLEN+1];
int messageID;
int AoMessages;
int ok;
};
dataStruct SD;
void Client::sendMessages() {
//...
send(this->getSocket(),reinterpret_cast<char *>(&SD),sizeof(SD),0);
//...
}
Code: Select all
struct dataStruct {
char message[SERVER_MAXLEN+1];
int messageID;
int AoMessages;
int ok;
};
dataStruct RD;
void Client::getMessage() {
//...
recv(this->getSocket(),reinterpret_cast<char *>(&RD),sizeof(RD),0);
//...
}
but the information I receive is like this:
Code: Select all
First recv:
messageID = 0
AoMessages = 0
ok = 0
message = "Message OK"
Second recv:
messageID = 10
AoMessages = 10
ok = 1
message = ""
Code: Select all
What it should be:
messageID = 10
AoMessages = 10
ok = 1
message = "Message OK"