Named pipe cross-process communication
客户端代码:
#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);//等待pipe的创建成功! BOOL bRet = WaitNamedPipe(TEXT("\\\\.\\Pipe\\mypipe"), NMPWAIT_WAIT_FOREVER); if (!bRet) { printf("connect the namedPipe failed!\n"); return 0; } HANDLE hPipe = CreateFile( //管道属于一种特殊的文件 TEXT("\\\\.\\Pipe\\mypipe"), //创建的文件名 GENERIC_READ | GENERIC_WRITE, //文件模式 0, //是否共享 NULL, //指向一个SECURITY_ATTRIBUTES结构的指针 OPEN_EXISTING, //创建参数 FILE_ATTRIBUTE_NORMAL, //文件属性(隐藏,只读)NORMAL为默认属性 NULL); //模板创建文件的句柄 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) //向服务器发送内容 { 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); //接受服务发送过来的内容 printf("From Server: data = %s, size = %d\n", rbuf, rlen); } Sleep(1000); } CloseHandle(hPipe);//关闭管道 } system("pause"); return 0; }
服务端代码:
#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"), //管道名 PIPE_ACCESS_DUPLEX, //管道类型 PIPE_TYPE_MESSAGE|PIPE_READMODE_MESSAGE|PIPE_WAIT, //管道参数 PIPE_UNLIMITED_INSTANCES, //管道能创建的最大实例数量 0, //输出缓冲区长度 0表示默认 0, //输入缓冲区长度 0表示默认 NMPWAIT_WAIT_FOREVER, //超时时间 NULL); //指定一个SECURITY_ATTRIBUTES结构,或者传递零值 if (INVALID_HANDLE_VALUE == hPipe) { printf("Create Pipe Error(%d)\n",GetLastError()); } else { printf("Waiting For Client Connection...\n"); if(!ConnectNamedPipe(hPipe, NULL)) //阻塞等待客户端连接。 { printf("Connection failed!\n"); } else { printf("Connection Success!\n"); } while (true) { if(!ReadFile(hPipe,buf,256,&rlen,NULL)) //接受客户端发送过来的内容 { 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); //向客户端发送内容 printf("To Client: data = %s, size = %d\n", wbuf, wlen); Sleep(1000); } } FlushFileBuffers(hPipe); DisconnectNamedPipe(hPipe); CloseHandle(hPipe);//关闭管道 } system("pause"); return 0; }
The above is the detailed content of Named pipe cross-process communication. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



What process is explorer.exe? When we use the Windows operating system, we often hear the term "explorer.exe". So, are you curious about what this process is? In this article, we will explain in detail what process explorer.exe is and its functions and effects. First of all, explorer.exe is a key process of the Windows operating system. It is responsible for managing and controlling Windows Explorer (Window

ccsvchst.exe is a common process file that is part of the Symantec Endpoint Protection (SEP) software, and SEP is an endpoint protection solution developed by the well-known network security company Symantec. As part of the software, ccsvchst.exe is responsible for managing and monitoring SEP-related processes. First, let’s take a look at SymantecEndpointProtection(

In Linux systems, zombie processes are special processes that have been terminated but still remain in the system. Although zombie processes do not consume many resources, if there are too many, they may cause system resource exhaustion. This article will introduce how to correctly remove zombie processes to ensure the normal operation of the system. 1Linux zombie process After the child process completes its task, if the parent process does not check the status in time, the child process will become a zombie process. The child process is waiting for confirmation from the parent process, and the system will not recycle it until it is completed. Otherwise, the zombie process will continue to hang in the system. To check whether there are zombie processes in the system, you can run the command top to view all running processes and possible zombie processes. The result of the ‘top’ command can be seen from the figure above in Linux.

Detailed explanation of the Linux process priority adjustment method. In the Linux system, the priority of a process determines its execution order and resource allocation in the system. Reasonably adjusting the priority of the process can improve the performance and efficiency of the system. This article will introduce in detail how to adjust the priority of the process in Linux and provide specific code examples. 1. Overview of process priority In the Linux system, each process has a priority associated with it. The priority range is generally -20 to 19, where -20 represents the highest priority and 19 represents

File reading and writing through pipes: Create a pipe to read data from the file and pass it through the pipe Receive the data from the pipe and process it Write the processed data to the file Use goroutines to perform these operations concurrently to improve performance

How to Pause Task Manager Process Updates in Windows 11 and Windows 10 Press CTRL+Window Key+Delete to open Task Manager. By default, Task Manager will open the Processes window. As you can see here, all the apps are endlessly moving around and it can be hard to point them down when you want to select them. So, press CTRL and hold it, this will pause the task manager. You can still select apps and even scroll down, but you must hold down the CTRL button at all times.

Why do processes in Linux sleep? In the Linux operating system, a process can become dormant due to a number of different reasons and conditions. When a process is in a dormant state, it means that the process is temporarily suspended and cannot continue execution until certain conditions are met before it can be awakened to continue execution. Next, we will introduce in detail several common situations when a process enters hibernation in Linux, and illustrate them with specific code examples. Waiting for I/O to complete: When a process initiates an I/O operation (such as reading

"Detection method of no PHP process in Linux system, specific code examples are required" When using Linux system for web development, we often rely on PHP process to handle dynamic pages and logic, and sometimes we may need to monitor whether there is a PHP process on the server. This article will introduce a method to detect whether there is a PHP process in a Linux system and give specific code examples. Why is it necessary to detect the PHP process? In web development, the PHP process plays a vital role. It is responsible for parsing and executing PHP processes.
