Код: Выделить всё
int main()
{
spdlog::set_pattern("[%H:%M:%S %z] [%^---%L---%$] [thread %t] %v");
Server server(9001);
int sockfd = server.start();
if (sockfd < 0)
{
spdlog::error("Cannot init server");
return 1;
}
while (true)
{
int fd = server.loop();
std::thread t(newclient, fd);
}
}
void newclient(int fd)
{
Handler handler;
handler.loop(fd);
closesocket(fd);
};
Код: Выделить всё
void Handler::loop(int fd){
std::string buffer;
Communication communication(fd);
while(true){
buffer.erase();
buffer = communication.recv_str();
if (buffer == "") {
continue;
}
}
...
}
Код: Выделить всё
std::string Communication::recv_str(){
std::vector buffer(CHUNK_SIZE);
std::string rcv;
long bytes_received = 0;
do {
bytes_received = recv(fd, &buffer[0], CHUNK_SIZE, 0);
if ( bytes_received < 0 ) {
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/78461757/recv-doesnt-work-in-a-thread-windows-c[/url]