使用在这里找到客户端和服务器的例子: http://www.winsocketdotnetworkprogramming.com/ winsock2programming / winsock2advancedmailslot14.html 用VS2008编译它们,运行的服务器,然后选择“客户端Myslot”我不断收到“WriteFail失败,错误53.”有人有想法么?链接到其他邮筒实例也欢迎,谢谢。

服务器:

    // Server sample
#include <windows.h>
#include <stdio.h>

void main(void)
{

    HANDLE Mailslot;
    char buffer[256];
    DWORD NumberOfBytesRead;

    // Create the mailslot

    if ((Mailslot = CreateMailslot("\\\\.\\Mailslot\\Myslot", 0, MAILSLOT_WAIT_FOREVER, NULL)) == INVALID_HANDLE_VALUE)
    {
        printf("Failed to create a mailslot %d\n", GetLastError());
        return;
    } 

    // Read data from the mailslot forever!

    while(ReadFile(Mailslot, buffer, 256, &NumberOfBytesRead, NULL) != 0)
    {
        printf("%.*s\n", NumberOfBytesRead, buffer);
    }
}

客户端:

// Client sample

#include <windows.h>
#include <stdio.h>

void main(int argc, char *argv[])
{
    HANDLE Mailslot;
    DWORD BytesWritten;
    CHAR ServerName[256];

    // Accept a command line argument for the server to send a message to

    if (argc < 2)
    {
        printf("Usage: client <server name>\n");
        return;
    }

    sprintf(ServerName, "\\\\%s\\Mailslot\\Myslot", argv[1]);

    if ((Mailslot = CreateFile(ServerName, GENERIC_WRITE,

        FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
    {
        printf("CreateFile failed with error %d\n", GetLastError());
        return;
    }

    if (WriteFile(Mailslot, "This is a test", 14, &BytesWritten, NULL) == 0)
    {
        printf("WriteFile failed with error %d\n", GetLastError());
        return;
    }

    printf("Wrote %d bytes\n", BytesWritten);
    CloseHandle(Mailslot);
}
有帮助吗?

解决方案

错误53是ERROR_BAD_NETPATH,“网络路径未找到”。显然,你正在使用的邮筒错误的服务器名称。如果服务器在同一台计算机客户端上运行使用\\.\mailslot\blah。而且不要忘了逃跑在字符串中的反斜杠:"\\\\.\\mailslot\\blah"

其他提示

我精确复制的代码张贴到两个文件,用VS2008编译他们,他们跑了完美。如果客户端程序被编译为client.exe,然后输入以下命令:

client .

client <computername>

其中计算机名称是PC的名称没有域。你可以调用API GetComputerName 检索名称。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top