1. Postal Slots
The mail slot is a one-way communication mechanism provided by Windows system, which can transmit data very small, usually around 400k.
Create a mail slot
// Specifies the name of the message slot, such as // ./mailslot/[path]name, the decimal point represents a server-based machine (cannot create a mail slot for a remote computer). DWORD nmaxmessagesize,// the maximum message length (in bytes) that can be written to the message slot, the client message is larger than the value the server does not accept the message, or 0 to receive any length of message. DWORD lreadtiemout,// wait mode and no wait mode, Mailslot_wait_forever waits indefinitely, 0 returns immediately, other values in milliseconds. lpsecurity_attributes lpsecurityattributes// access control permissions, generally set to null);
Instance
Service side
1#include <Windows.h>2#include <stdio.h>3 intMain ()4 {5HANDLE Mail;//define a mail slot handle6Mail=createmailslot ("\\\\.\\mailslot\\newslot",0, mailslot_wait_forever,null);//Create a mail slot7 if(Mail==invalid_handle_value)//determine the handle of a mail slot8 {9printf"failed to create mail slot! \ r \ n");TenCloseHandle (mail);//Close Handle One } A Else - { -printf"mail slot created successfully! \ r \ n"); the Chartext[ $]; -DWORD ReadText;//get the actual read value - if(ReadFile (Mail,text, $, &readtext,null))//reading Data - { + printf (text); - } + Else A { atprintf"\ r \ n Read data failure!\r\n"); - } -CloseHandle (mail);//Close Handle -Sleep ( +); - } - in return 0; -}
Client
1#include <Windows.h>2#include <stdio.h>3 intMain ()4 {5HANDLE Mail2;//Mail slot handle6 Chartext[]="I'm going to send a message.";7 DWORD writetext;8Mail2=createfile ("\\\\.\\mailslot\\newslot", generic_write,file_share_read,null,open_existing,file_attribute_normal,null);//Open File/create file9 if(INVALID_HANDLE_VALUE==MAIL2)//deciding whether to open successfullyTen { Oneprintf"Mail Slot open failed! \ r \ n"); A - } - Else the { - if(WriteFile (Mail2,text,sizeof(text), &writetext,null))//Write Data - { -Sleep ( -); +printf"Data write success \ r \ n"); - } + Else A { atSleep ( -); -printf"Mail Slot Write failed! \ r \ n"); - } -CloseHandle (MAIL2);//Close Mail Slots - } -Sleep (10000); in return 0; -}
VC + + Multithreading-inter-process communication