Answer the question
In order to leave comments, you need to log in
Does it throw an error when the thread terminates?
I have a client-server application. The client types some commands, they are sent to the server and then processed by this function:
void Server::ClientThread(SOCKET ClientConn)
{
int ID = -1, r;
char Func[32];
std::map<std::string, void(Server::*)(SOCKET, int)>::iterator It;
while ((r = recv(ClientConn, Func, sizeof(Func) - 1, NULL)) != -1 && r != 0)
if ((It = Functions.find(Func)) != Functions.end())
(this->*It->second)(ClientConn, ID);
std::cout << "Client disconnected\n";
if (ID != -1) DeleteUser(ID);
}
static unsigned int __stdcall _Call_func(void *_Data)
{ // entry point for new thread
static_cast<_Pad *>(_Data)->_Go();
return (0);
}
Answer the question
In order to leave comments, you need to log in
_Data == nullptr, so it crashes.
The debugger shows the NEXT line, after which the error occurred. Those. error before return. And there are only two options, either _Data == nullptr, or something went wrong in the _Go () function and it’s further poking around in the debugger :)
The program is simply written incorrectly. recv can return any number of bytes, from 0 to 31. At the same time, your array is not initialized, and it is not clear whether it will contain 0.
When you try to search in the map, you get a string from the array of characters, the end of which is determined by the presence of zero. And zero may not be, in the end - a fall.
Moreover, having read only a piece of data, recv will read the continuation of this data on the next reading, that is, instead of the beginning of the next command, you will get the middle of the previous one, and as a result - complete garbage.
You need to rewrite the reading of commands and rewrite the protocol so that the length of the next command is transmitted at the beginning of this very command.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question