【问题标题】:Simple Mailslot program not working?简单的邮槽程序不起作用?
【发布时间】:2010-03-14 20:42:42
【问题描述】:

使用此处找到的客户端和服务器示例:http://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancedmailslot14.html 使用 VS2008 编译它们,运行服务器,然后运行“客户端 Myslot”,我不断收到“WriteFail failed with error 53”。有人有想法么?也欢迎提供其他 Mailslot 示例的链接,谢谢。

服务器:

    // 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);
}

【问题讨论】:

    标签: windows visual-studio-2008 writefile mailslot


    【解决方案1】:

    错误 53 是 ERROR_BAD_NETPATH,“找不到网络路径”。显然,您为邮槽使用了错误的服务器名称。如果服务器与您的客户端在同一台机器上运行,请使用\\.\mailslot\blah。并且不要忘记在字符串中转义反斜杠:"\\\\.\\mailslot\\blah"

    【讨论】:

    • 你在哪里找到错误的解释?使用 \\.\mailslot\myslot 现在给我 161。
    • 引号/反斜杠的任何组合仍然返回 161 错误。
    • 啊,新的错误代码。 161=ERROR_BAD_PATHNAME。我从这里看不到你在做什么。
    • 打开 2 个命令提示符,从一个运行服务器,然后在另一个运行客户端 \\.\Mailslot\myslot,从而产生错误
    • @Shawn:正如所写,您必须将服务器名称作为命令行参数提供。像“客户”。注意点。通过不使用 argv 和硬编码服务器名称来更容易测试。
    【解决方案2】:

    我将发布的代码完全复制到两个文件中,用 VS2008 编译它们,它们运行完美。如果您的客户端程序编译为 client.exe,则键入以下命令:

    client .
    

    client <computername>
    

    其中计算机名称是不带域的 PC 名称。您可以调用 API GetComputerName 来检索名称。

    【讨论】:

      猜你喜欢
      • 2018-08-15
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      • 1970-01-01
      相关资源
      最近更新 更多