named pipe

const char* const PIPE_NAME = "\\\\.\\pipe\\test";
const int MAX_CONNECTIONS   = 10;

void client_main()
{
    DWORD last_error;
    unsigned int elapsed_seconds       = 0;
    const unsigned int timeout_seconds = 5;

    HANDLE handle = CreateFile(PIPE_NAME,
                               GENERIC_READ | GENERIC_WRITE,
                               0,
                               0,
                               OPEN_EXISTING,
                               FILE_ATTRIBUTE_NORMAL,
                               0);

    while (INVALID_HANDLE_VALUE == handle &&
           elapsed_seconds < timeout_seconds)
    {
        last_error = GetLastError();

        if (last_error != ERROR_PIPE_BUSY)
        {
            break;
        }

        Sleep(1 * 1000);
        elapsed_seconds++;

        handle = CreateFile(PIPE_NAME,
                            GENERIC_READ | GENERIC_WRITE,
                            0,
                            0,
                            OPEN_EXISTING,
                            FILE_ATTRIBUTE_NORMAL,
                            0);
    }

    if (INVALID_HANDLE_VALUE == handle)
    {
        std::cerr << "Failed to connect to pipe " << PIPE_NAME <<
            ": last_error=" << last_error << "\n";
    }
    else
    {
        std::cout << "Connected to pipe " << PIPE_NAME << "\n";
        CloseHandle(handle);
    }
}

HANDLE _get_server_handle()
{
    // Error handling omitted for security descriptor creation.
    SECURITY_DESCRIPTOR sd;
    InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
    SetSecurityDescriptorDacl(&sd, TRUE, static_cast<PACL>(0), FALSE);

    SECURITY_ATTRIBUTES sa;
    sa.nLength              = sizeof(sa);
    sa.lpSecurityDescriptor = &sd;
    sa.bInheritHandle       = FALSE;

    // Create a bi-directional message pipe.
    HANDLE handle = CreateNamedPipe(PIPE_NAME,
                                    PIPE_ACCESS_DUPLEX,
                                    PIPE_TYPE_MESSAGE       |
                                      PIPE_READMODE_MESSAGE |
                                      PIPE_NOWAIT,
                                    PIPE_UNLIMITED_INSTANCES,
                                    4096,
                                    4096,
                                    0,
                                    &sa);

    if (INVALID_HANDLE_VALUE == handle)
    {
        std::cerr << "Failed to create named pipe handle: last_error=" <<
            GetLastError() << "\n";
    }

    return handle;
}

void server_main()
{
    HANDLE handle = _get_server_handle();

    if (INVALID_HANDLE_VALUE != handle)
    {
        int count = 0;
        while (count < MAX_CONNECTIONS)
        {
            BOOL result = ConnectNamedPipe(handle, 0);

            const DWORD last_error = GetLastError();

            if (ERROR_NO_DATA == last_error)
            {
                count++;
                std::cout << "A client connected and disconnected: count=" <<
                    count << "\n";
                CloseHandle(handle);
                handle = _get_server_handle();
            }
            else if (ERROR_PIPE_CONNECTED == last_error)
            {
                count++;
                std::cout << "A client connected before call to " <<
                    "ConnectNamedPipe(): count=" << count << "\n";
                CloseHandle(handle);
                handle = _get_server_handle();
            }
            else if (ERROR_PIPE_LISTENING != last_error)
            {
                std::cerr << "Failed to wait for connection: last_error=" <<
                    GetLastError() << "\n";
                CloseHandle(handle);
                break;
            }
            Sleep(100);
        }
    }
}

int main(int a_argc, char** a_argv)
{
    if (2 == a_argc)
    {
        if (std::string("client") == *(a_argv + 1))
        {
            for (int i = 0; i < MAX_CONNECTIONS; i++)
            {
                client_main();
            }
        }
        else if (std::string("server") == *(a_argv + 1))
        {
            server_main();
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值