日志
管道通信实例
2017-05-02 16:46
A程序作为服务器,不断从B程序接收数据,并发送到C程序中:
#include <stdio.h> #include <conio.h> #include <tchar.h> #include <Windows.h> #include <process.h> #include <stdlib.h> const char *pStrPipeNameGet = "\\\\.\\pipe\\Name_pipe_demon_get"; const char *pStrPipeNameSend = "\\\\.\\pipe\\Name_pipe_demon_send"; const int BUFFER_MAX_LEN = 1024; char buf[BUFFER_MAX_LEN]; DWORD dwLen; HANDLE get, mSend, mutex; LPCRITICAL_SECTION cs; WCHAR* toWChar(const char *c) { WCHAR wszClassName[256]; memset(wszClassName, 0, sizeof(wszClassName)); MultiByteToWideChar(CP_ACP, 0, c, strlen(c) + 1, wszClassName,\ sizeof(wszClassName) / sizeof(wszClassName[0])); return wszClassName; } void beginGetThread(PVOID p) { printf("服务器Get\n"); printf("等待连接......\n"); HANDLE hPipe = CreateNamedPipe(toWChar(pStrPipeNameGet),\ PIPE_ACCESS_DUPLEX,PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,\ PIPE_UNLIMITED_INSTANCES, 0, 0, NMPWAIT_WAIT_FOREVER, 0); if (ConnectNamedPipe(hPipe, NULL) != NULL)//等待连接。 { printf("连接成功,开始接收数据\n"); while (true) { WaitForSingleObject(mutex, INFINITE); EnterCriticalSection(cs);//接收客户端发送的数据 ReadFile(hPipe, buf, BUFFER_MAX_LEN, &dwLen, NULL); printf("接收到来自A的数据长度为%d字节\n", dwLen); printf("具体数据内容如下:"); int bufSize; for (bufSize = 0; bufSize < (int)dwLen; bufSize++) { putchar(buf[bufSize]); } LeaveCriticalSection(cs); Sleep(500); ReleaseSemaphore(mutex, 1, NULL); putchar('\n'); } } else { printf("连接失败\n"); } DisconnectNamedPipe(hPipe); CloseHandle(hPipe);//关闭管道 } void beginSendThread(PVOID p) { printf("服务器Send\n"); printf("等待连接......\n"); HANDLE hPipe = CreateNamedPipe(toWChar(pStrPipeNameSend),PIPE_ACCESS_DUPLEX,\ PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,\ PIPE_UNLIMITED_INSTANCES, 0, 0, NMPWAIT_WAIT_FOREVER, 0); if (ConnectNamedPipe(hPipe, NULL) != NULL)//等待连接。 { printf("连接成功,开始发送缓冲区数据至B\n"); while (true) { WaitForSingleObject(mutex, INFINITE); EnterCriticalSection(cs); WriteFile(hPipe, buf, (int)dwLen, &dwLen, NULL); LeaveCriticalSection(cs);Sleep(500); ReleaseSemaphore(mutex, 1, NULL); } } else { printf("连接失败\n"); } DisconnectNamedPipe(hPipe); CloseHandle(hPipe);//关闭管道 } int _tmain(int argc, _TCHAR* argv[]) { cs = (LPCRITICAL_SECTION)malloc(sizeof(LPCRITICAL_SECTION)); InitializeCriticalSection(cs); mutex = CreateSemaphore(NULL, 1, 1, TEXT("mutex")); _beginthread(beginGetThread, NULL, NULL); _beginthread(beginSendThread, NULL, NULL); Sleep(INFINITE);DeleteCriticalSection(cs); return 0; } B程序不断接收从键盘输入的数据,数据以回车结束,并发送给A #include <stdio.h> #include <tchar.h> #include <Windows.h> #include <conio.h> const char *pStrPipeName = "\\\\.\\pipe\\Name_pipe_demon_get"; const int BUFFER_MAX_LEN = 1024; char buf[BUFFER_MAX_LEN]; int _tmain(int argc, _TCHAR* argv[]) { printf("按任意键以开始连接Get\n"); _getch(); printf("A开始等待......\n"); if (!WaitNamedPipe(pStrPipeName, NMPWAIT_WAIT_FOREVER)) { printf("Error! 连接Get失败\n"); return 0; } HANDLE hPipe = CreateFile(pStrPipeName, GENERIC_WRITE, 0,NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); while (true) { printf("请输入要向服务端发送的数据,回车键结束,最大1024个字节\n"); DWORD dwLen = 0; int bufSize; for (bufSize = 0; bufSize < BUFFER_MAX_LEN; bufSize++) { buf[bufSize] = getchar(); if (buf[bufSize] == '\n') break; }//向服务端发送数据 if (WriteFile(hPipe, buf, bufSize, &dwLen, NULL)) { printf("数据写入完毕共%d字节\n", dwLen); } else { printf("数据写入失败\n"); } Sleep(1000); } CloseHandle(hPipe); return 0; } C程序接收到从A发送来的数据,并转换成大写写入文件 #include <stdio.h> #include <tchar.h> #include <Windows.h> #include <conio.h> const char *pStrPipeName = "\\\\.\\pipe\\Name_pipe_demon_send"; const int BUFFER_MAX_LEN = 1024; char buf[BUFFER_MAX_LEN]; DWORD dwLen = 0; int _tmain(int argc, _TCHAR* argv[]) { printf("按任意键以开始连接Send\n"); _getch(); printf("B开始等待......\n"); if (!WaitNamedPipe(pStrPipeName, NMPWAIT_WAIT_FOREVER)) { printf("Error! 连接Send失败\n"); return 0; } HANDLE hPipe = CreateFile(pStrPipeName, GENERIC_READ, 0,NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); while (true) {// 接收服务端发回的数据 ReadFile(hPipe, buf, BUFFER_MAX_LEN, &dwLen, NULL);//读取管道中的内容(管道是一种特殊的文件) printf("接收服务端发来的信息,长度为%d字节\n", dwLen); printf("具体数据内容如下:"); HANDLE hWrite = CreateFile(_T("data.txt"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); for (int j = 0; j <= (int )dwLen; j++) { putchar(buf[j]); buf[j] = toupper(buf[j]); } putchar('\n'); SetFilePointer(hWrite, NULL, NULL, FILE_END); WriteFile(hWrite, buf, (int)dwLen, NULL, NULL); WriteFile(hWrite, "\n", 1, NULL, NULL); CloseHandle(hWrite); Sleep(1000); } CloseHandle(hPipe); return 0; } |
下一篇: 无
上一篇: 进程间通信——管道