管道通信

在看网络编程时遇到了管道通信问题,便找了些资料来学习:

客户端

#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 code

	LPVOID 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 process

	lpDisplayBuf = (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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值