管道通信
来源:互联网 发布:阿里云人工智能平台 编辑:程序博客网 时间:2024/05/16 17:15
在看网络编程时遇到了管道通信问题,便找了些资料来学习:
客户端
#include "stdio.h"#include "windows.h"char info[1000]={0};void showInfo(unsigned char * data,int len){char buf[32]={0};for(int i=0;i<len;i++){sprintf(&buf[i*3],"%02x ",data[i]);}strcat(info,buf);}int main(){HANDLE hInput=::GetStdHandle(STD_INPUT_HANDLE);HANDLE hOuput=::GetStdHandle(STD_OUTPUT_HANDLE);DWORD buf[2],tmp;::MessageBox(NULL,TEXT("before readfile"),NULL,MB_OK);ReadFile(hInput,buf,sizeof(buf),&tmp,NULL);::MessageBox(NULL,TEXT("after readfile"),NULL,MB_OK);strcat(info,"buf:");showInfo((unsigned char *)buf,sizeof(buf));strcat(info," ,tmp:");showInfo((unsigned char *)&tmp,sizeof(tmp));DWORD res=buf[0]+buf[1];strcat(info," ,res:");showInfo((unsigned char *)&res,sizeof(res));::MessageBox(NULL,info,NULL,MB_OK);WriteFile(hOuput,&res,sizeof(res),&tmp,NULL);return 0;}
服务器
#include "stdio.h"#include "windows.h"#include "tchar.h"#include <strsafe.h>void ErrorExit(LPTSTR lpszFunction) { // Retrieve the system error message for the last-error codeLPVOID lpMsgBuf;LPVOID lpDisplayBuf;DWORD dw = GetLastError(); FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_IGNORE_INSERTS,NULL,dw,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR) &lpMsgBuf,0, NULL );// Display the error message and exit the processlpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR)); StringCchPrintf((LPTSTR)lpDisplayBuf, LocalSize(lpDisplayBuf),TEXT("%s failed with error %d: %s"), lpszFunction, dw, lpMsgBuf); MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); LocalFree(lpMsgBuf);LocalFree(lpDisplayBuf);}int main(){SECURITY_ATTRIBUTES sa;sa.nLength=sizeof(sa);sa.bInheritHandle=true;sa.lpSecurityDescriptor=NULL;HANDLE hRead,hWrite;if(!CreatePipe(&hRead,&hWrite,&sa,0)){ErrorExit(TEXT("createpipe"));}STARTUPINFO si={sizeof(si)};si.hStdInput=hRead;si.hStdOutput=hWrite;si.hStdError=hWrite;si.wShowWindow=SW_HIDE;si.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;PROCESS_INFORMATION pi;TCHAR strCmdLine[]=TEXT("bit24.exe");if(!CreateProcess(NULL,strCmdLine,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi)){ErrorExit(TEXT("createprocess"));}DWORD data[]={100,5},res,tmp;WriteFile(hWrite,data,sizeof(data),&tmp,NULL);//FlushFileBuffers(hWrite);system("pause");ReadFile(hRead,&res,sizeof(res),&tmp,NULL);_tprintf(TEXT("%d\n"),res);CloseHandle(pi.hThread);CloseHandle(pi.hProcess);return 0;}
这样写完后若注释掉system("pause"),则父进程从管道里读取的数据是父进程自己写入管道里的数据data[0],因为父进程和子进程共享同一个管道(通过SECURITY_ATTRIBUTES.bInheritHandle和CreateProcess的bInheritHandles的参数均为true来实现),都可以从管道里读取和写入数据,貌似需要同步来协调执行的顺序,所有我这里用 system("pause")使父进程停下了,网上其他的一些实例都是在创建子进程后CloseHandle(hWritePipe),在si.hStdOutput中指定其他hFile,然后父进程在从hFile中读取数据。
http://www.002pc.com/master/College/Language/VC/2010-05-12/14017.html
http://www.pediy.com/bbshtml/bbs8/pediy8-724.htm
http://hi.baidu.com/ilotus_y/blog/item/d05c674384532e1673f05d83.html
http://www.cnblogs.com/ahuo/archive/2007/01/08/614723.html
http://www.newasp.net/tech/program/21101.html
- 管道通信
- 管道通信
- 管道通信
- 管道通信
- 管道通信
- 管道通信
- 管道通信
- 管道通信
- 管道通信
- 管道通信 .
- 管道通信
- 管道通信
- 管道通信
- 管道通信
- 管道通信
- 管道通信
- 管道通信
- 管道通信
- java--小型登入系统代码
- 子曾经说过,在有些问题上,我们决不能退步
- HTTP状态码
- linux 常用命令: 查看系统信息
- android 笔记--返回homescreen
- 管道通信
- Windows Azure SDK 1.5、Windows Azure Tools for Microsoft Visual Studio 2010和新的服务管理功能发布了
- 手机 省份证 归属地查询接口
- Linux下配置两个或多个Tomcat启动
- 递增链表的交集
- 数学电影推荐
- 实习日记043(9月21日)
- 在Visual C++ 6.0下显示JPEG、GIF等格式标准的图像的一种实现起来比较简便的方法
- ActiveMQ 初步入门及相关概念理解