简体   繁体   English

连接命名为 pipe

[英]Connection to named pipe

I tried using named pipes in C++ to transfer a message from one computer to another.我尝试在 C++ 中使用命名管道将消息从一台计算机传输到另一台计算机。 But the client could not connect to the server, it returned error 1326 (invalid name or password), although the server name was specified correctly.但是客户端无法连接到服务器,它返回错误 1326(无效的名称或密码),尽管服务器名称指定正确。

What does the password have to do with the channel?密码和频道有什么关系? And if there are no errors in the code, then what else could be causing the connection error to the channel?如果代码中没有错误,那么还有什么可能导致通道连接错误?

Also, on the Internet, I saw such a form recording the channel name \\servername\pipe\[path] pipename .另外,我在网上看到这样的表格记录了频道名\\servername\pipe\[path] pipename servername is the name of the server, pipe determines that it is a channel, and pipename is the name of the channel, but what is [path] ? servername是服务器的名字, pipe判断是频道, pipename是频道的名字,但是[path]是什么?

Server服务器

#include <windows.h>
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    HANDLE hNamedPipe;
    DWORD dwBytesRead; 
    DWORD dwBytesWrite; 
    char pchMessage[80];
    int nMessageLength;

    PSECURITY_DESCRIPTOR psd;
    SECURITY_ATTRIBUTES sa;
    psd = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
    InitializeSecurityDescriptor(psd, SECURITY_DESCRIPTOR_REVISION);
    SetSecurityDescriptorDacl(psd, true, NULL, false);
    sa.nLength = sizeof(sa);
    sa.bInheritHandle = true;
    sa.lpSecurityDescriptor = psd;

    hNamedPipe= CreateNamedPipe("\\\\.\\pipe\\pipe", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | WRITE_OWNER, PIPE_READMODE_MESSAGE | PIPE_TYPE_MESSAGE| PIPE_ACCEPT_REMOTE_CLIENTS, PIPE_UNLIMITED_INSTANCES, 2048, 2048, INFINITE, &sa);
    if (hNamedPipe == INVALID_HANDLE_VALUE)
    {
            cerr << "Create named pipe failed." << endl
            << "The last error code: " << GetLastError() << endl;
        cout << "Press any key to exit.";
        cin.get();

        return 0;
    }
    cout << "The server is waiting for connection with a client." << endl;
    if (!ConnectNamedPipe(
        hNamedPipe,
        NULL 
    ))
    {
        cerr << "Connect named pipe failed." << endl
            << "The last error code: " << GetLastError() << endl;
        CloseHandle(hNamedPipe);
        cout << "Press any key to exit.";
        cin.get();

        return 0;
    }
    if (!ReadFile(
        hNamedPipe,
        pchMessage,
        sizeof(pchMessage),
        &dwBytesRead, 
        NULL))
    {
        cerr << "Data reading from the named pipe failed." << endl
            << "The last error code: " << GetLastError() << endl;
        CloseHandle(hNamedPipe);
        cout << "Press any key to exit.";
        cin.get();

        return 0;
    }

    cout << "The server received the message from a client: "
        << endl << '\t' << pchMessage << endl;

    cout << "Input a string: ";
    cin.getline(pchMessage, 80);
    nMessageLength = strlen(pchMessage) + 1;

        if (!WriteFile(
            hNamedPipe,
            pchMessage,
            nMessageLength,
            &dwBytesWrite,
            NULL
        ))
        {
            cerr << "Write file failed." << endl
                << "The last error code: " << GetLastError() << endl;
            CloseHandle(hNamedPipe);
            cout << "Press any key to exit.";
            cin.get();

            return 0;
        }

    cout << "The server sent the message to a client: "
        << endl << '\t' << pchMessage << endl;

    CloseHandle(hNamedPipe);
    cout << "Press any key to exit.";
    cin.get();

    return 0;
}

Client客户

#include <windows.h>
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string machineName;
    getline(cin, machineName, '\n');
    machineName= "\\\\"+machineName+"\\pipe\\pipe";
    cout << machineName << endl;
    char pipeName[80];
    strcpy_s(pipeName, machineName.c_str());
    HANDLE hNamedPipe;
    DWORD dwBytesWritten;
    DWORD dwBytesRead;
    char pchMessage[80];
    int nMessageLength;

    hNamedPipe = CreateFile(
        pipeName, // имя канала
        GENERIC_READ | GENERIC_WRITE, 
        FILE_SHARE_READ | FILE_SHARE_WRITE, 
        NULL, 
        OPEN_EXISTING, 
        FILE_ATTRIBUTE_NORMAL, 
        NULL);

    if (hNamedPipe == INVALID_HANDLE_VALUE)
    {
        cerr << "Connection with the named pipe failed." << endl
            << "The last error code: " << GetLastError() << endl;
        cout << "Press any key to exit.";
        cin.get();
        return 0;
    }
    cin.get();
    cout << "Input a string: ";
    cin.getline(pchMessage, 80);
    nMessageLength = strlen(pchMessage) + 1;
    if (!WriteFile(
        hNamedPipe, 
        pchMessage, 
        nMessageLength,
        &dwBytesWritten,
        NULL)) 
    {
            cerr << "Write file failed: " << endl
            << "The last error code: " << GetLastError() << endl;
        CloseHandle(hNamedPipe);
        cout << "Press any key to exit.";
        cin.get();
        return 0;
    }
        cout << "The client sent the message to a server: "
        << endl << '\t' << pchMessage << endl;
    if (!ReadFile(
        hNamedPipe, 
        pchMessage,
        sizeof(pchMessage), 
        &dwBytesRead,
        NULL))
    {
        cerr << "Read file failed: " << endl
            << "The last error code: " << GetLastError() << endl;
        CloseHandle(hNamedPipe);
        cout << "Press any key to exit.";
        cin.get();
        return 0;
    }
    cout << "The client received the message from a server: "
        << endl << '\t' << pchMessage << endl;
    CloseHandle(hNamedPipe);
    cout << "Press any key to exit.";
    cin.get();
    return 0;
}

After my testing, there are two places in your code that need to be corrected.经过我的测试,您的代码中有两个地方需要更正。

The NetUseAdd function establishes a connection between the local computer and a remote server. NetUseAdd function 在本地计算机和远程服务器之间建立连接。 You can specify a local drive letter or a printer device to connect.您可以指定要连接的本地驱动器号或打印机设备。 If you do not specify a local drive letter or printer device, the function authenticates the client with the server for future connections.如果您未指定本地驱动器号或打印机设备,function 将向服务器验证客户端以供将来连接。

In addition, file and print sharing need be turned on.此外,需要打开file and print sharing

Refer: SMB named pipe with no connection restrictions参考: SMB 命名为 pipe 无连接限制

After everything is configured, the code can work normally.一切配置好后,代码就可以正常工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM