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.