[Inter-process communication] interprocess communication is implemented using the oil tank.

Source: Internet
Author: User

Tank is

1: designed based on Broadcast Communication

2: it uses connectionless and unreliable data transmission

3: one-way communication

4: the server process in the oil tank reads data, and the client process writes data.

5: the message length is less than 424 bytes (about 210 Chinese characters)

 

Steps:

1: create a single MFC Appwizard named mailslotsrvProgram, Add a menu item named "receive data", idm_mailslot_rece, and add command response for the menu in view

2: Write the following in the response function:Code,

Void cmailslotsrvview: onreceive () {// todo: add your command handler code herehandle hmailslot; // create a slot handle hmailslot = createmailslot ("\\\\. \ Mailslot \ mymailslot ", 0, mailslot_wait_forever, null); // create a slot. For details, see msdnif (invalid_handle_value = hmailslot) // If the slot fails to be created, prompt User {MessageBox ("failed to create mail slot"); return;} Char Buf [100]; // read data DWORD dwread; If (! Readfile (hmailslot, Buf, 100, & dwread, null) {MessageBox ("failed to read data"); closehandle (hmailslot); // close the tank handle hmailslot = NULL; return;} MessageBox (BUF); closehandle (hmailslot); // if the data is read successfully or fails, close the slot handle to avoid calling it again during structure analysis .}

3: Add a new MFC Appwizard project named mailslotclt, which is in the same file directory (parallel directory) as mailslotsrv. Add a menu named "send data" and add command response.

4: Add the following code in the command response function,

Void cmailslotcltview: onsend () {// todo: add your command handler code here // broadcast-based communication handle hmailslot; hmailslot = createfile ("\\\\. \ Mailslot \ mymailslot ", generic_write, file_share_read, null, open_existing, file_attribute_normal, null); // only data can be sent if (invalid_handle_value = hmailslot) {MessageBox ("failed to open the mail slot"); return;} Char Buf [] = "Hello, Mailslot"; DWORD dwwrite; If (! Writefile (hmailslot, Buf, strlen (BUF) + 1, & dwwrite, null) {MessageBox ("failed to write data"); closehandle (hmailslot); return ;} closehandle (hmailslot );}

5: Compile and run the program separately.

Note: first, click "receive data" on the server side, and then click "send data" on the client side ". If the sequence is reversed, the program cannot run normally, because the oil tank is not created and communication is unnecessary.

 

This is the communication between two processes (one-way)

 

 

How can I read and write data in the same process?

It is very easy to write both the server and client of the oil tank in a program, use the server to receive data, and use the client to send data.

 

We can use the broadcast feature of the oil tank to create a conference notification system. In contrast, the program code volume is very small. If the same system is implemented using socket, it will be quite complicated.

Install an oil tank server program on the computers of all employees and install an oil tank client program on the computer of the management personnel to implement meeting notifications. If you want to publish a meeting Information, you only need to send this information in the customer program, then all employees will receive this notification at the same time. In this way, a simple one-to-many one-way communication is formed.

 

The same as the named pipe, the Oil Slot not only can communicate between processes on a host, but also can communicate across networks. For clipboard and anonymous pipelines, they can only communicate between processes on the same machine.

The Oil Slot can implement one-to-many communication, but the amount of data transmitted is very small. You can use pipelines to transmit large amounts of data.

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.