简单的邮槽程序不起作用?
使用此处找到的客户端和服务器示例: 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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误 53 是 ERROR_BAD_NETPATH,“未找到网络路径”。显然您为邮槽使用了错误的服务器名称。如果服务器与客户端在同一台计算机上运行,请使用
\\.\mailslot\blah
。并且不要忘记转义字符串中的反斜杠:"\\\\.\\mailslot\\blah"
。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"
.我将代码完全按照发布的内容复制到两个文件中,用 VS2008 编译它们,它们运行得很好。如果您的客户端程序编译为 client.exe,则键入以下命令:
或
其中计算机名称是不带域的 PC 名称。您可以调用 API GetComputerName 来检索名称。
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:
or
where computer name is the PC's name without the domain. You can call the API GetComputerName to retrieve the name.