-3

我创建了我的管道服务器,它使用函数 ConnectNamedPipe 创建了一个管道实例。之后我从管道中读取数据并将数据保存到缓冲区中。我还没有创建我的客户,但我必须做一些操作。

我的客户必须向管道写入 2 件事: 1 - 服务器将执行的操作 - 减法、乘法、加法、除法(我正在尝试实现一种计算器) 2 - 服务器将计算的 2 个数字

我的服务器必须从管道中读取这些操作和 2 个数字并在屏幕上打印结果。

所以,我的问题是,我如何解析客户端编写的这两个操作?

我的服务器工作正常,但解析有问题。

#include <iostream>
#include <Windows.h>

using namespace std;

int main()
{
    HANDLE createPipe;
    BOOL Connect;
    BOOL Read;
    int buffer[100];
    DWORD numBytesRead;

    //Create Pipe
    createPipe = CreateNamedPipe(
        L"\\\\.\\pipe\\StackOverflow",
        PIPE_ACCESS_DUPLEX,
        PIPE_TYPE_MESSAGE,
        PIPE_UNLIMITED_INSTANCES,
        1024,
        1024,
        NMPWAIT_USE_DEFAULT_WAIT,
        NULL);

    //Check for failure
    if(createPipe == INVALID_HANDLE_VALUE){
        cout<<"Failed to create a pipe! "<<endl;
    }

    //Create instance of the pipe
    Connect = ConnectNamedPipe(
        createPipe,
        NULL);

    //Check for failure
    if(!(Connect)){
        cout<<"Failed to connect to the pipe"<<endl;
        CloseHandle(createPipe);
    }

    //Read bytes from the buffer
    Read = ReadFile(
            createPipe, 
            buffer,
            99 * sizeof(buffer),
            &numBytesRead,
            NULL);

    //check for failure
    if(!(Read)){
        cout<<"Failed to read from the pipe"<<endl;
        CloseHandle(createPipe);
    }

    return 0;
}

谢谢

4

1 回答 1

0

您应该研究序列化 - 您应该如何对管道数据进行编码和解码。你有一个 Calculation 对象,它有两个 Operand 元素(数字)和一个 Operation(可能是枚举?) - 那么问题就变成了如何序列化(发送)和反序列化(接收器)该对象,而不依赖于管道 I/O。

查看 Boost.Serialization 的概念 - boost.org/doc/libs/1_49_0/libs/serialization/doc/index.html

于 2012-04-19T14:58:09.763 回答