簡體   English   中英

命名管道客戶端錯誤5(C ++)

[英]Named Pipes Client Error 5 (C++)

好了,下面我有命名管道服務器/客戶端的代碼。 我將服務器放置在Windows 8計算機上,它創建了一個管道,然后等待客戶端連接。 然后,我在Windows 7上啟動客戶端,但它返回錯誤5(我認為訪問被拒絕?)。有人可以向我解釋為什么它給我錯誤5嗎?

預先考慮所有答案

服務器代碼

#include "stdafx.h"
#include "windows.h"
#include <iostream> 

using namespace std; 

#define g_szPipeName "\\\\.\\pipe\\pipename"  //Name given to the pipe


#define BUFFER_SIZE 1024 //1k
#define ACK_MESG_RECV "Message received successfully"
#define _CRT_SECURE_NO_WARNINGS 


HANDLE hPipe;

int repeate() {
    char szBuffer[BUFFER_SIZE];
    DWORD cbBytes;

    //We are connected to the client.
    //To communicate with the client we will use ReadFile()/WriteFile() 
    //on the pipe handle - hPipe

    //Read client message
    BOOL bResult = ReadFile(
        hPipe,                // handle to pipe 
        szBuffer,             // buffer to receive data 
        sizeof(szBuffer),     // size of buffer 
        &cbBytes,             // number of bytes read 
        NULL);                // not overlapped I/O 

    if ((!bResult) || (0 == cbBytes))
    {
        printf("\nError occurred while reading from the client: %d", GetLastError());
        CloseHandle(hPipe);
        system("Pause");
        return 1;  //Error
    }
    else
    {
        printf("\nReadFile() was successful.");
    }

    printf("\nClient sent the following message: %s", szBuffer);

    strcpy(szBuffer, ACK_MESG_RECV);

    //Reply to client
    bResult = WriteFile(
        hPipe,                // handle to pipe 
        szBuffer,             // buffer to write from 
        strlen(szBuffer) + 1,   // number of bytes to write, include the NULL 
        &cbBytes,             // number of bytes written 
        NULL);                // not overlapped I/O 

    if ((!bResult) || (strlen(szBuffer) + 1 != cbBytes))
    {
        printf("\nError occurred while writing to the client: %d", GetLastError());
        CloseHandle(hPipe);
        system("Pause");
        return 1;  //Error
    }
    else
    {
        printf("\nWriteFile() was successful.");
    }
    repeate();

}



int main(int argc, char* argv[])
{

    SECURITY_DESCRIPTOR sd;
    InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);

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


    hPipe = CreateNamedPipe(
        g_szPipeName,             // 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  
        BUFFER_SIZE,              // output buffer size 
        BUFFER_SIZE,              // input buffer size 
        NMPWAIT_USE_DEFAULT_WAIT,
        &sa);




        if (INVALID_HANDLE_VALUE == hPipe)
        {
            printf("\nError occurred while creating the pipe: %d", GetLastError());
            system("Pause");
            return 1;  //Error
        }
        else
        {
            printf("\nCreateNamedPipe() was successful.");
        }

        printf("\nWaiting for client connection...");

        //Wait for the client to connect
        BOOL bClientConnected = ConnectNamedPipe(hPipe, NULL);

        if (FALSE == bClientConnected)
        {
            printf("\nError occurred while connecting to the client: %d", GetLastError());
            CloseHandle(hPipe);
            system("Pause");
            return 1;  //Error
        }
        else
        {
            printf("\nConnectNamedPipe() was successful.");
        }


        repeate();

    }

客戶代碼

#include "stdafx.h" 
#include "windows.h"
#include <iostream>

#define g_szPipeName "\\\\MyComputerName\\pipe\\pipename"  //Name given to the pipe 


#define BUFFER_SIZE 1024 //1k
#define ACK_MESG_RECV "Message received successfully"

HANDLE hPipe;

int repeate() {
    char szBuffer[BUFFER_SIZE];

    printf("\nEnter a message to be sent to the server: ");
    gets(szBuffer);

    DWORD cbBytes;

    //Send the message to server
    BOOL bResult = WriteFile(
        hPipe,                // handle to pipe 
        szBuffer,             // buffer to write from 
        strlen(szBuffer) + 1,   // number of bytes to write, include the NULL
        &cbBytes,             // number of bytes written 
        NULL);                // not overlapped I/O 

    if ((!bResult) || (strlen(szBuffer) + 1 != cbBytes))
    {
        printf("\nError occurred while writing to the server: %d", GetLastError());
        CloseHandle(hPipe);
        system("Pause");
        return 1;  //Error
    }
    else
    {
        printf("\nWriteFile() was successful.");
    }

    //Read server response
    bResult = ReadFile(
        hPipe,                // handle to pipe 
        szBuffer,             // buffer to receive data 
        sizeof(szBuffer),     // size of buffer 
        &cbBytes,             // number of bytes read 
        NULL);                // not overlapped I/O 

    if ((!bResult) || (0 == cbBytes))
    {
        printf("\nError occurred while reading from the server: %d", GetLastError());
        CloseHandle(hPipe);
        system("Pause");
        return 1;  //Error
    }
    else
    {
        printf("\nReadFile() was successful.");
    }

    printf("\nServer sent the following message: %s", szBuffer);
    repeate();

}

int main(int argc, char* argv[])
{


     //Connect to the server pipe using CreateFile()
     hPipe = CreateFile( 
          g_szPipeName,   // pipe name 
          GENERIC_READ |  // read and write access 
          GENERIC_WRITE, 
          0,              // no sharing 
          NULL,           // default security attributes
          OPEN_EXISTING,  // opens existing pipe 
          0,              // default attributes 
          NULL);          // no template file 

     if (INVALID_HANDLE_VALUE == hPipe) 
     {
          printf("\nError occurred while connecting to the server: %d", GetLastError()); 
          //One might want to check whether the server pipe is busy
          //This sample will error out if the server pipe is busy
          //Read on ERROR_PIPE_BUSY and WaitNamedPipe() for that
          system("Pause");
          return 1;  //Error
     }
     else
     {
          printf("\nCreateFile() was successful.");
     }

     //We are done connecting to the server pipe, 
     //we can start communicating with the server using ReadFile()/WriteFile() 
     //on handle - hPipe


     repeate(); 
}

通過添加IP地址,子網掩碼,默認網關來配置兩台計算機。 從網絡和共享中心,打開所有共享,然后關閉密碼保護的共享。 通過從cmd ping IP地址對其進行測試。 兩台計算機的用戶帳戶均不應使用密碼保護。 這就是為什么我修復了Client Error 5並在兩台遠程計算機之間建立命名管道通信的原因

客戶端應使用OpenFile(),而不是CreateFile()。 客戶端不想創建任何新東西,它想與現有管道通信。

暫無
暫無

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

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