views:

225

answers:

2

Using the client and server examples found here: http://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancedmailslot14.html Compiling them with VS2008, running the server and then "client Myslot" I keep getting "WriteFail failed with error 53." Anyone have any ideas? Links to other Mailslot examples are also welcome, thanks.

Server:

    // 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:

// 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  A: 

Error 53 is ERROR_BAD_NETPATH, "The network path was not found". Clearly you are using the wrong server name for the mailslot. Use \\.\mailslot\blah if the server runs on the same machine as your client. And don't forget to escape the backslash in a string: "\\\\.\\mailslot\\blah".

Hans Passant
Where did you find the explanation of the error? Using \\.\mailslot\myslot gives me 161 now.
Shawn
Any combination of quotes/backslashes still returns the 161 error.
Shawn
Ah, new error code. 161=ERROR_BAD_PATHNAME. I cannot see what you're doing from here.
Hans Passant
opening up 2 command prompts, running server from one and then client \\.\Mailslot\myslot in the other, thus producing the error
Shawn
Trivially explained by not escaping the backslashes correctly. This isn't going to go anywhere until you post your code or figure it out by yourself.
Hans Passant
client \\\\.\\Mailslot\\Myslot produces the same error. I said I was using the examples in the link. :) Thats the code.
Shawn
I still cannot see the server name from your snippet, you are using a command line argument. What is argv[1]?
Hans Passant
I have no idea, this is the code on the website that I copied exactly into 2 .cpp files, compiled and then ran.
Shawn
@Shawn: as written, you have to give the server name as a command line argument. Like "client ." note the dot. It is easier to test this by not using argv and hard-coding the server name.
Hans Passant
Ah well that's easy then.
Shawn
A: 

I copied the code exactly as posted into two files, compiled them with VS2008 and they ran perfectly. If your client program is compiled as client.exe, then type the following command:

client .

or

client <computername>

where computer name is the PC's name without the domain. You can call the API GetComputerName to retrieve the name.

Mark Wilkins