[Communication between IPC processes] MailSlot

Source: Internet
Author: User

IPC inter-process communication + mail slot MailSlotIPC (Inter-Process Communication ).Use of modern computersVirtual Memory Mechanism, IsProcessProvides an independent enough address space for security purposes. A process does not have special permissions and cannot access the memory space of another process. The process is isolated from each other. Inter-process communication (IPC) requires a special mechanism. MailSlot is one of the commonly used IPC methods.1. MailSlot):The MailSlot communication process is dividedServerAndClient. The server creates a MailSlot and specifies the mail slot name during creation. The client opens MailSlot with the same mail slot name.The mail trough is unidirectional.The server can only read MailSlot, and the client can only write MailSlot. The server reads data first in first out, that is, the data written first is read first.
2. mail slot name:Local format: \. \ mailslot \ [path \] nameFor example: \. mailslot \ my_mailslot
3. Basic API functions:Create a mailbox:HANDLE CreateMailSlot (Lptstr lpName,DWORD nMaxMessageSize,DWORD lReadTimeout,LPSECURITY_ATTRIBUTES lpSecurityAttributes );The first parameter is the mail slot name.The second parameter indicates the maximum message length.Third parameter: Read timeout. If it is set to 0, no message is returned immediately. When MAILSLOT_WAIT_FORVER, the message remains waiting.Third parameter: Security Attribute, which is generally set to NULL.This function creates a Mailslot and returns the handle of the mailbox.Read the mail trough:BOOL ReadFile (
HANDLE hFile,
LPVOID lpBuffer,
DWORD nNumberOfBytesToRead,
LPDOWRD lpNumberOfBytesRead,
LPOVERLAPPED lpOverlapped );
The first parameter is the mail slot handle.The second parameter is the cache address.The third parameter is the message length.Fourth parameter: the actual read length.Fifth parameter: generally set to NULL.This function is used to read data in the mailbox.Open the mailbox:BOOL CreateFile (Lptstr lpFileName,
DWORD dwDesiredAccess,
DWORD dw1_mode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile
);The first parameter is the file pointer.The second parameter is access mode.Third parameter: Shared Mode.Fourth parameter: Security Attribute pointer.Fifth parameter: Create option.Sixth parameter: file attributes.Seventh parameter: used to copy the file handle.This function is used by the client to open Mailslot.Write to the mailbox:BOOL WriteFile (
HANDLE hFile,
LPCVOID lpBuffer,
DWORD nNumberOfBytesToWrite,
LPDWORD lpNumberOfBytesWritten,
LPOVERLAPPED lpOverlapped );
The first parameter is the file handle.The second parameter is the data buffer pointer.Third parameter: number of bytes written.Fourth parameter: returns the actual number of bytes written.Fifth parameter: struct pointer, usually NULL.This function is used by the client to write data to Mailslot.
4. Cow test:Run the server program in VC6.0 and then run the client program:
 Running effect:
Mailslot Server:Mailslot client:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.