Named pipeline tour

Source: Internet
Author: User

The example program server is a service program on the Windows platform. For more information about the service program, see:

Http://blog.csdn.net/felixz/article/details/1346380

Http://blog.csdn.net/rptrpt/article/details/2849654

Documentation on service programs:

Http://download.csdn.net/detail/tornodo/4227671

Name the MPs queue. A process puts data in the MPs queue. Other processes that know the MPs queue name can take the data away. The server only needs to call the createnamedpipe function to create an MPS queue. To prevent blocking, start a thread and process client connections and data sending and receiving in the thread. Prepare a pipeline in the thread and wait for the client connection to be achieved through the connectnamedpipe function, that is, listen to the client connection.

The client uses waitnamedpipe to make the server process wait for the client to connect to the instance. Before the timeout value changes to 0, if the server has a pipeline ready for connection, this function returns true (in this example, the connection timeout of the function I set is permanent nmpwait_wait_forever, so that the function will return only when the connection is successful, without judging its return value ), then you can call createfile to connect to the MPs queue. At this time, the server accepts the connection from the client. The connectnamedpipe function returns true, and the createfile of the client returns the handle pointing to the pipeline. Communication is implemented through readfile and writefile. You can use the server to call disconnectnamedpipe and the client to call closefile.

Here I encapsulate a class and put the operation code of the server and the client in it. The code for creating a named pipeline on the server is as follows:

Bool cmypipe: servercreatenamedpipe () {const tchar szpipename [] = text ("\\\\. \ PIPE \ ssnp \ "); // creates a named pipe for receiving messages. m_hpipe = createnamedpipe (szpipename, pipe_access_duplex | file_flag_write_through, // blocking mode. pipe_wait | pipe_type_byte, pipe_unlimited_instances, 512,512, null, null); // check whether the named pipe is created. if (m_hpipe = invalid_handle_value) {return false;} hrhread = (handle) _ beginthreadex (null, 0, (pthread) serverreadproc, this, 0, null); Return true ;}

Server processing thread code:

// Server thread DWORD winapi serverreadproc (lpvoid) {DWORD dwnumbytesread, dwnumbyteswrite; tchar todisptxt [512]; cmypipe * parent = (cmypipe *) lpvoid; // check the message repeatedly until the program ends. while (1! = Interlockedcompareexchange (& bstop, 1,1) {If (! Connectnamedpipe (parent-> m_hpipe, null) {continue;} // read the message and check whether the data is read successfully. while (readfile (parent-> m_hpipe, todisptxt, sizeof (todisptxt), & signature, null) {_ tcscpy_s (todisptxt, _ countof (todisptxt), parent-> m_strreply ); // write back a string. writefile (parent-> m_hpipe, todisptxt, sizeof (todisptxt), & dwnumbyteswrite, null); sleep (1000);} disconnectnamedpipe (parent-> m_hpipe);} return 0 ;}

The server sends the current system memory usage status to the MPs queue every second. After the client receives the message successfully, a value is returned, indicating that the message is successfully received.

Server Installation

At startup

Display of the client when the service is stopped

Project: workshop. The code for the server to start the client is given at the beginning of two links, which are described in. I remember that after the server started the client in Windows 32, it could be displayed normally, I changed the 64-bit win7 system the other day. Today, I took this out and found that the client must run as administrator to display it normally. Do you remember wrong ~~~~

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.