Mail slot for Windows inter-process communication

Source: Internet
Author: User

 

The mail trough is designed based on the broadcast communication system and uses non-connection and unreliable data transmission. The mail trough is a one-way communication mechanism. The server process that creates the mail trough reads data and the client process that opens the oil trough writes data. To ensure that the slot works normally on various Windows platforms, we should limit the message length to less than 424 bytes during message transmission.

The mail trough server side and client side are relatively simple to write, and the program is provided directly:

Server:

Void cmailslotsrvview: onmailslotrecv ()
{
// Todo: add the command handler code here
Handle hmailslot;
Hmailslot = createmailslot ("\\\\\ Mailslot \ mymailslot", 0, mailslot_wait_forever, null); // create a mail slot
If (invalid_handle_value = hmailslot)
{
MessageBox ("An error occurred while creating the mailbox! ");
Return;
}
Char Buf [100];
DWORD dwread;
If (! Readfile (hmailslot, Buf, 100, & dwread, null ))
{
MessageBox ("failed to read data! ");
Closehandle (hmailslot );
Return;
}
MessageBox (BUF );
Closehandle (hmailslot );
}

 

Client:

Void cmailslotcltview: onmailslotsend ()
{
// Todo: add the command handler code here
Handle hmailslot;
Hmailslot = createfile ("\\\\\\ Mailslot \ mymailslot", generic_write, file_assist_read, null, open_existing, file_attribute_normal, null); // open the mailbox
If (invalid_handle_value = hmailslot)
{
MessageBox ("failed to open the mailbox ");
Return;
}
Char Buf [] = "centennial celebration of Shandong University ";
DWORD dwwrite;
If (! Writefile (hmailslot, Buf, strlen (BUF) + 1, & dwwrite, null ))
{
MessageBox ("failed to write data! ");
Closehandle (hmailslot );
Return;
}
Closehandle (hmailslot );
}

It can be seen that the mail slot can only implement one-way communication. If you want to use the mail slot, you can use a process to read or send data, you only need to write a server and a client in a program to receive and send data.

Related Article

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.