#include <windows.h>
#include <stdio.h>
//邮槽服务器
void main(void)
{
HANDLE Mailslot;
char buffer[256];
DWORD NumberOfBytesRead;
// Create the mailslot
if ((Mailslot = CreateMailslot((LPCWSTR)"\\\\.\\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)
{
buffer[NumberOfBytesRead] = 0;
printf("%s\n", buffer);
}
}
//邮槽客户机
#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((LPCWSTR)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);
}
服务器返回Failed to create a mailslot 123
客户机返回Usage: client <server name>
刚接触网络编程,不知道为什么返回这样子的结果,Kernel32.lib已经添加,两个进程在同一台机器上运行。求指导如何才能让这两个进程相互通信,客户机能向服务器发送信息。