Загрузка данных


#include <windows.h>
#include <iostream>
#include <string>

using namespace std;

const char* PIPE_NAME = "\\\\.\\pipe\\PilgrimagePipe";

int main()
{
    setlocale(LC_ALL, "Russian");

    char buffer[256];
    DWORD bytesRead;
    DWORD bytesWritten;
    HANDLE hPipe;

    if (!WaitNamedPipeA(PIPE_NAME, NMPWAIT_WAIT_FOREVER))
    {
        cout << "Server is not available." << endl;
        return 1;
    }

    hPipe = CreateFileA(
        PIPE_NAME,
        GENERIC_READ | GENERIC_WRITE,
        0,
        NULL,
        OPEN_EXISTING,
        0,
        NULL
    );

    if (hPipe == INVALID_HANDLE_VALUE)
    {
        cout << "Failed to connect to server." << endl;
        return 1;
    }

    cout << "Connected to server." << endl;

    int pilgrimId;
    int patienceMs;

    cout << "Введите id паломника: ";
    cin >> pilgrimId;

    cout << "Введите терпение в миллисекундах: ";
    cin >> patienceMs;

    cin.ignore();

    string request = to_string(pilgrimId) + " " + to_string(patienceMs);

    BOOL writeOk = WriteFile(
        hPipe,
        request.c_str(),
        (DWORD)request.size(),
        &bytesWritten,
        NULL
    );

    if (!writeOk)
    {
        cout << "Failed to write to server." << endl;
        CloseHandle(hPipe);
        return 1;
    }

    cout << "Sent: " << request << endl;

    BOOL readOk = ReadFile(
        hPipe,
        buffer,
        sizeof(buffer) - 1,
        &bytesRead,
        NULL
    );

    if (!readOk || bytesRead == 0)
    {
        cout << "Failed to read from server." << endl;
        CloseHandle(hPipe);
        return 1;
    }

    buffer[bytesRead] = '\0';
    string response = buffer;

    cout << "Response from server: " << response << endl;

    CloseHandle(hPipe);
    return 0;
}