VC + + Multithreading-inter-process communication

Source: Internet
Author: User
Tags readfile

1. Postal Slots

The mail slot is a one-way communication mechanism provided by Windows system, which can transmit data very small, usually around 400k.

Create a mail slot

Specifies the name of the mail slot, such as //./mailslot/[path]name, where the decimal point represents the server-based machine (cannot create a mail slot for a remote computer). DWORD nmaxmessagesize,//The maximum message length (in bytes) that can be written to the message slot, the client message is larger than the value the server does not accept the message, or 0 to receive any length of message. DWORD lreadtiemout,// wait mode and no wait mode, Mailslot_wait_forever waits indefinitely, 0 returns immediately, other values in milliseconds. Lpsecurity_attributes lpsecurityattributes// access control permissions, generally set to null);        

Instance

Service side

1 #include <Windows.h>2 #include <stdio.h>3IntMain ()4{5 HANDLE Mail;//Define a mail slot handle6 Mail=createmailslot ("\\\\.\\mailslot\\newslot",0,mailslot_wait_forever,null);//Create a mail slot7if (Mail==invalid_handle_value)//Determine the handle of a mail slot8{9 printf ("Failed to create mail slot! \ r \ n");Ten CloseHandle (mail);//Close handle11}12Else13{printf ("Mail slot created successfully! \ r \ n");15Char text[200];DWORD ReadText;//Get the actual read value17if (ReadFile (Mail,text,200,&readtext,null))//Reading data18{19printf (text);20}21stelse22  {23 printf (" \r\n read data failed!\r\n" ); 24 }25 closehandle (mail); // close handle 26 Sleep (1000); 27 }28 29 return 0;< Span style= "COLOR: #008080" >30}            

Client

1 #include <Windows.h>2 #include <stdio.h>3IntMain ()4{5 HANDLE Mail2;//Mail slot handle6Char text[]="I'm going to send a message.";7DWORD WRITETEXT;8 Mail2=createfile ("\\\\.\\mailslot\\newslot", generic_write,file_share_read,null,open_existing,file_attribute_normal,null);//Open File/Create file9if (INVALID_HANDLE_VALUE==MAIL2)//Deciding whether to open successfully10{printf ("Mail Slot open failed! \ r \ n");1213}14Else15{16if (WriteFile (Mail2,text,sizeof (text), &writetext,null))//Write Data17{Sleep (100);printf ("Data write success \ r \ n");20}21stElse22 {23 Sleep (10024 printf ( " mail slot Write failed! \r\n " }26 CloseHandle (MAIL2); // close mail slot 27 }28 Sleep (10000 29 return 0; 30}             

2. Named Pipes

A named pipe is a mechanism that can communicate between two processes on the same machine and two processes on different machines in a network. Named pipes transmit data in a connection-based and reliable way, so named pipes transmit data only one-to-one transmissions.

HANDLE WINAPI createnamedpipe (     lpctstr lpname,      DWORD Dwopenmode,      DWORD Dwpipemode,      DWORD Nmaxinstances,      DWORD Noutbuffersize,      DWORD Ninbuffersize,      DWORD ndefaulttimeout,      Lpsecurity_ ATTRIBUTES lpsecurityattributes);

Instance:

Service side

1 #include <Windows.h>2 #include <stdio.h>3IntMain ()4{5HANDLE Hpip;6 OVERLAPPED ovi={0};//Defining struct-Body variables7Char buf[200];//Defining data buffers8 DWORD Readbuf;//Gets the actual number of bytes read9 Hpip=createnamedpipe ("\\\\.\\pipe\\newpipe", Pipe_access_duplex,pipe_type_byte,pipe_unlimited_instances,1024,1024,0,null);//Creating pipelinesTen printf ("Create pipeline successfully, waiting for client connection ... \ r \ n");11if (Connectnamedpipe (Hpip,&ovi))//Waiting for a connection request from the client12{printf ("Client Connection succeeded! \ r \ n");printf ("Reading data! \ r \ n");15if (ReadFile (Hpip,buf,200,&readbuf,null))//Reading pipe Data16{printf ("Data Read Success! \ r \ n");printf ("The data read is:%s\r\n", buf);19}20Else21st{printf ("Data read failed! \ r \ n");23 }24 }25 else26  {27 printf ( " Client connection failed, please try again ... \r\n"  }29 system ( "pause "return 0;31}              

Client

1 #include <Windows.h>2 #include <stdio.h>3IntMain ()4{5HANDLE Hpipe;6 OVERLAPPED ovi={0};7Char buf[200]="Named pipe test";8DWORD Writebuf;9 printf ("Connecting pipe ... \ r \ n");10if (Waitnamedpipe ("\\\\.\\pipe\\newpipe", Nmpwait_wait_forever))//Connecting Named pipes11{printf ("Pipeline Connection Successful!");printf ("\ r \ n");Hpipe=createfile ("\\\\.\\pipe\\newpipe", generic_read| Generic_write,0,null,open_existing,file_attribute_normal,null);//Open the develop named pipe15if (hpipe==INVALID_HANDLE_VALUE)16{printf ("Failed to open the pipe! \ r \ n");18}19Else20{21stif (WriteFile (Hpipe,buf,200,&writebuf,null))//Writing data to Named pipes22{printf ("Data Write Success! \ r \ n");24}25 else26  {27 printf ("  Data write failed, please try again ... \r\n "); 28 }29 }30 }31 system" ( Span style= "COLOR: #800000" > "pause" ); 32 return 0; 33}             

3. Anonymous Pipeline

Anonymous pipes are unnamed pipes that can only communicate between parent and child processes, and anonymous pipelines cannot be used between network processes compared to named pipes.

asynchronous I/O mode

In socket programming, the asynchronous I/O pattern means that the program will call the relevant response function to process the message when there is an associated socket message in the network, otherwise the program will continue to wait for the relevant message to arrive or implement other operations in the background of the system.

VC + + Multithreading-inter-process communication

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.