代码之家  ›  专栏  ›  技术社区  ›  Shawn

简单的邮件槽程序不工作?

  •  0
  • Shawn  · 技术社区  · 16 年前

    使用此处的客户端和服务器示例: http://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancedmailslot14.html 用vs2008编译它们,运行服务器,然后运行“client myslot”,我一直得到“writefail failed with error 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);
    }
    
    2 回复  |  直到 16 年前
        1
  •  1
  •   Hans Passant    16 年前

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

        2
  •  1
  •   Mark Wilkins    16 年前

    我把发布的代码完全复制到两个文件中,用vs2008编译,运行得很好。如果客户端程序编译为client.exe,请键入以下命令:

    client .
    

    client <computername>
    

    其中computer name是没有域的PC的名称。你可以调用api GetComputerName 以检索名称。

    推荐文章