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"