簡體   English   中英

C ++命名管道網絡無效名稱錯誤

[英]C++ Named Pipes over network invalid name error

我試圖通過命名管道在兩個Windows PC之間發送消息。 在本地調用CreateNamedPipe ,一切正常。 如果我將主機名從"\\\\\\\\.\\\\pipe\\\\testpipename"更改為"\\\\\\\\myHostname\\\\pipe\\\\testpipename"則會從getLastError()獲取ERROR_INVALID_NAME(123) getLastError()

這是我的代碼:

    BOOL   fConnected = FALSE;
    DWORD  dwThreadId = 0;
    HANDLE hPipe = INVALID_HANDLE_VALUE, hThread = NULL;
    LPTSTR pipeName = /*nullptr*/ TEXT("\\\\myHostname\\pipe\\testpipename");

    SECURITY_ATTRIBUTES sa = { 0 };
    SECURITY_DESCRIPTOR sd = { 0 };

    InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);

    SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE);

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

    hPipe = CreateNamedPipe(
    pipeName,                 // pipe name  
    PIPE_ACCESS_DUPLEX,       // read/write access 
    PIPE_TYPE_MESSAGE |       // message type pipe 
    PIPE_READMODE_MESSAGE |   // message-read mode 
    PIPE_WAIT,                // blocking mode 
    PIPE_UNLIMITED_INSTANCES, // max. instances  
    255,                      // output buffer size 
    255,                      // input buffer size 
    0,                        // client time-out 
    &sa);                     // default security attribute 

    if (hPipe == INVALID_HANDLE_VALUE)
    {
        cout << GetLastError();
        return -2;
    }

    cout << "Waiting for client to connect!" << endl;

    //waiting for client to connect
    fConnected = ConnectNamedPipe(hPipe, NULL) ?
        TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);

    cout << "Client connected! YEAH" << endl;

我的猜測是,pipename無效,但我不知道為什么。 有任何想法嗎?

問題解決了! 服務器和客戶端的Pipenames是:

服務器: "\\\\\\\\.\\\\pipe\\\\testpipe"
客戶端: "\\\\\\\\serverHostName\\\\pipe\\\\testpipe"

客戶也做了一些小的改動。 完整的代碼可以在我的Github回購中找到。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM