2010-03-14 83 views
0

使用这里找到的客户端和服务器示例:http://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancedmailslot14.html使用VS2008编译它们,运行服务器,然后运行“客户端Myslot”我不断收到“WriteFail失败,错误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); 
} 

回答

1

错误53 ERROR_BAD_NETPATH, “网络路径找不到”。显然你使用错误的服务器名称作为邮筒。如果服务器与客户端在同一台计算机上运行,​​请使用\\.\mailslot\blah。不要忘记在字符串中跳出反斜杠:"\\\\.\\mailslot\\blah"

+0

你从哪里找到错误的解释?使用\\。\ mailslot \ myslot现在给我161。 – Shawn 2010-03-14 23:24:55

+0

任何引号/反斜杠的组合仍然会返回161错误。 – Shawn 2010-03-14 23:36:34

+0

嗯,新的错误代码。 161 = ERROR_BAD_PATHNAME。我从这里看不到你在做什么。 – 2010-03-14 23:47:45

1

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

client . 

client <computername> 

,其中计算机名是计算机的名称没有域。您可以拨打API GetComputerName来检索名称。