管道通信

来源:互联网 发布:阿里云人工智能平台 编辑:程序博客网 时间: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

原创粉丝点击