//服务端
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE Mailslot;
TCHAR buffer[256]={0};
DWORD NumberOfBytesRead=0;
if((Mailslot = CreateMailslot(L"\\\\.\\Mailslot\\Myslot", 0, MAILSLOT_WAIT_FOREVER, NULL))
== INVALID_HANDLE_VALUE)
{
printf("Failed to create a mailslot %d\n", GetLastError());
return 0;
}
while(ReadFile(Mailslot, buffer, 256, &NumberOfBytesRead, NULL) != 0)
{
printf("%d is %s\n", NumberOfBytesRead, buffer);
}
system("pause");
return 0;
}
//客户端
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE Mailslot=0;
DWORD BytesWritten=0;
TCHAR ServerName[256]={0};
wcscpy(ServerName, L"\\\\.\\Mailslot\\Myslot");
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 0;
}
if(WriteFile(Mailslot, "This is a test", sizeof("This is a test") , &BytesWritten, NULL) == 0)
{
printf("WriteFile failed with error %d\n", GetLastError());
return 0;
}
printf("Wrote %d bytes\n", BytesWritten);
CloseHandle(Mailslot);
system("pause");
return 0;
}