C使用Windows命名管道

前端之家收集整理的这篇文章主要介绍了C使用Windows命名管道前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
由于某些原因,桅杆和奴隶都失败了,但是我可以找到关于它们如何工作的任何好例子,所以我不知道我哪里出错了.

主服务器在ConnectNamedPipe之后永远不会退出WaitForSingleObject,并且从服务器在第一个boost :: asio :: read调用中抛出异常,“等待进程打开管道的另一端”,我虽然WaitNamedPipe的意思是等待主人中的ConnectNamedPipe?

master.cpp

asio::io_service ioservice; 
asio::windows::stream_handle in(ioservice); 
int main()
{
    HANDLE pipe = INVALID_HANDLE_VALUE;
    try
    {
        //create pipe
        pipe = CreateNamedPipe("\\\\.\\pipe\\FLTest",PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE | FILE_FLAG_OVERLAPPED,PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,255,50000,0);
        if(pipe == INVALID_HANDLE_VALUE)
        {
            printWinError();
            return -1;
        }
        in.assign(pipe); 
        std::cout << "Created pipe" << std::endl;
        //spawn child
        STARTUPINFO         startInfo;
        ZeroMemory(&startInfo,sizeof(STARTUPINFO));
        startInfo.cb = sizeof(STARTUPINFO);
        PROCESS_INFORMATION procInfo;
        ZeroMemory(&procInfo,sizeof(PROCESS_INFORMATION));
        if(CreateProcess(0,"slave.exe",FALSE,CREATE_NEW_CONSOLE,&startInfo,&procInfo))
        {
            std::cout << "Slave process created" << std::endl;
        }
        else
        {
            printWinError();
            DisconnectNamedPipe(pipe);
            return -1;
        }

        OVERLAPPED overlapped = {0};
        overlapped.hEvent = CreateEvent(0,TRUE,0);
        if(ConnectNamedPipe(pipe,&overlapped) == FALSE)
        {
            unsigned error = GetLastError();
            if(error != ERROR_PIPE_CONNECTED &&
                error != ERROR_IO_PENDING)
            {
                printWinError();
                DisconnectNamedPipe(pipe);
                return -1;
            }
        }
        WaitForSingleObject(overlapped.hEvent,INFINITE);
        CloseHandle(overlapped.hEvent);
        std::cout << "Pipe connected" << std::endl;

        for(int i = 0; i < 100; ++i)
        {
            boost::system::error_code error;
            unsigned n = i * 5;
            asio::write(in,asio::buffer((char*)&n,sizeof(unsigned)),asio::transfer_all(),error);
            if(error)throw boost::system::system_error(error);
        }
        std::cout << "Sent data" << std::endl;

        FlushFileBuffers(pipe);
        DisconnectNamedPipe(pipe);
        system("pause");
        return 0;
    }
    catch(const std::exception &e)
    {
        std::cout << e.what() << std::endl;
        if(pipe != INVALID_HANDLE_VALUE)
            DisconnectNamedPipe(pipe);
        system("pause");
        return -1;
    }
}

slave.cpp

asio::io_service ioservice; 
asio::windows::stream_handle in(ioservice); 
int main()
{
    try
    {
        WaitNamedPipe("\\\\.\\pipe\\FLTest",NMPWAIT_WAIT_FOREVER);

        std::cout << "Pipe avaible" << std::endl;
        HANDLE pipe = CreateNamedPipe("\\\\.\\pipe\\FLTest",PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,0);
        if(pipe == INVALID_HANDLE_VALUE)
        {
            printWinError();
            return -1;
        }

        in.assign(pipe); 
        std::cout << "Pipe connected" << std::endl;

        for(int i = 0; i < 100; ++i)
        {
            std::cout << "i: " << i << std::endl;
            boost::system::error_code error;
            unsigned n;
            asio::read(in,error);
            if(error)throw boost::system::system_error(error);
        }
        system("pause");
        return 0;
    }
    catch(const std::exception &e)
    {
        std::cout << e.what() << std::endl;
        system("pause");
        return -1;
    }
}

显然我有一些完全错误,但我在网上找不到任何东西来比较我的代码.

解决方法

在您的奴隶中,您需要调用CreateFile()来打开管道,而不是CreateNamedPipe.
HANDLE pipe = CreateFile("\\\\.\\pipe\\FLTest",GENERIC_READ | GENERIC_WRITE,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL
      );

猜你在找的C&C++相关文章