2010-03-14 86 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來檢索名稱。