2012-04-18 31 views
-3

我创建了我的管道服务器,它使用函数ConnectNamedPipe创建管道实例 管道。之后,我从管道读取数据并保存到缓冲区。 我还没有创建我的客户端,但我必须做一些操作。如何解析通过管道接收的消息

我的客户写信给管两件事: 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; 
} 

THX

+0

这是功课? – sashoalm 2012-04-18 16:58:29

+0

我希望..我想这样做因为它可以帮助我提高我的编程技巧 – user1341970 2012-04-18 16:59:46

+0

好的。你的头衔说问题是通过管道传递数据。所以你用客户端发送数据,但没有到达服务器,对吗? – sashoalm 2012-04-18 17:03:50

回答

0

你应该研究系列化 - 你是如何编码和解码管道数据应被封装。你有一个计算对象有两个操作数元素(数字)和一个操作(枚举也许?) - 然后问题变成你如何序列化(发送)和反序列化(接收器)该对象,独立于管道I/O。对于概念

退房Boost.Serialization - boost.org/doc/libs/1_49_0/libs/serialization/doc/index.html

相关问题