管道通信

在看網絡編程時遇到了管道通信問題,便找了些資料來學習:

客戶端

#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

發佈了14 篇原創文章 · 獲贊 1 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章