Named Pipeline Cross-process communication, pipeline process Communication
Client code:
# Include "stdafx. h "# include <stdio. h> # include <windows. h ># include <ctime> int main (int argc, _ TCHAR * argv []) {srand (time (NULL); DWORD wlen = 0; Sleep (1000 ); // wait until pipe is created successfully! BOOL bRet = WaitNamedPipe (TEXT ("\\\\\ Pipe \ mypipe"), NMPWAIT_WAIT_FOREVER); if (! BRet) {printf ("connect the namedPipe failed! \ N "); return 0;} HANDLE hPipe = CreateFile (// pipeline belongs to a special file TEXT ("\\\\. \ Pipe \ mypipe "), // The Name Of The created file is GENERIC_READ | GENERIC_WRITE, // file Mode 0, // whether to share NULL, // the pointer to a SECURITY_ATTRIBUTES structure is OPEN_EXISTING, // create the parameter FILE_ATTRIBUTE_NORMAL, // The file property (hidden, read-only) NORMAL is the default property NULL); // The template creates the file handle if (INVALID_HANDLE_VALUE = hPipe) {printf ("open the exit pipe failed! \ N ") ;}else {while (true) {char buf [256] =" "; sprintf (buf," % s % d ", buf, rand () % 1000); if (WriteFile (hPipe, buf, sizeof (buf), & wlen, 0) = FALSE) // send content {printf ("write to pipe failed! \ N "); break;} else {printf (" To Server: data = % s, size = % d \ n ", buf, wlen ); char rbuf [256] = ""; DWORD rlen = 0; ReadFile (hPipe, rbuf, sizeof (rbuf), & rlen, 0 ); // receives the content printf ("From Server: data = % s, size = % d \ n", rbuf, rlen);} Sleep (1000 );} closeHandle (hPipe); // closes the pipeline} system ("pause"); return 0 ;}
Server code:
# Include "stdafx. h "# include <stdio. h> # include <windows. h ># include <ctime> int main (int argc, _ TCHAR * argv []) {srand (time (NULL); char buf [256] = ""; DWORD rlen = 0; HANDLE hPipe = CreateNamedPipe (TEXT ("\\\\. \ Pipe \ mypipe "), // pipeline name PIPE_ACCESS_DUPLEX, // pipeline type PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, // pipeline parameter PIPE_UNLIMITED_INSTANCES, // The maximum number of instances that a media transcoding queue can create is 0. // The length of the output buffer is 0, indicating 0 by default. // The length of the input buffer is 0, indicating N by default. MPWAIT_WAIT_FOREVER, // time-out NULL); // specify a SECURITY_ATTRIBUTES structure, or pass the NULL value if (INVALID_HANDLE_VALUE = hPipe) {printf ("Create Pipe Error (% d) \ n ", GetLastError ();} else {printf (" Waiting For Client Connection... \ n "); if (! ConnectNamedPipe (hPipe, NULL) // blocking waits for client connection. {Printf ("Connection failed! \ N ");} else {printf (" Connection Success! \ N ") ;}while (true) {if (! ReadFile (hPipe, buf, 256, & rlen, NULL) // receives the content sent From the client {printf ("Read Data From Pipe Failed! \ N "); break;} else {printf (" From Client: data = % s, size = % d \ n ", buf, rlen ); char wbuf [256] = ""; sprintf (wbuf, "% s % d", wbuf, rand () % 1000); DWORD wlen = 0; WriteFile (hPipe, wbuf, sizeof (wbuf), & wlen, 0); // send the content printf ("To Client: data = % s, size = % d \ n", wbuf, wlen); Sleep (1000) ;}} FlushFileBuffers (hPipe); DisconnectNamedPipe (hPipe); CloseHandle (hPipe); // close pipeline} system ("pause "); return 0 ;}
Next, write down C # Name Pipe Cross-process communication