《实验四使用命名管道实现进程通信(共6页).doc》由会员分享,可在线阅读,更多相关《实验四使用命名管道实现进程通信(共6页).doc(6页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上学生姓名:张鹏学号:实验地点:数计学院407实验室实验课时:3学时实验器材:计算机课程名称: 计算机操作系统实验名称: 2.4实验四:使用命名管道实现进程通信一、实验目的1、进一步掌握windows系统环境下进程通信机制。2、熟悉进程通信API二、实验环境及工具Windows 7 操作系统,VC6三、 实验内容 使用Windows系统提供的命名管道完成两个进程之间的通信,要求能正确使用创建命名管道CreateNamePipe()、连接命名管道ConnectNamePipe()、拆除命名管道的连接DisconnectNamePipe()、连接服务器已建立的命名管道Cal
2、lNamePipe()、等待命名管道WaitNamePipe()等API.四、 实验步骤服务端/ PipeServer.cpp : Defines the entry point for the console application./#include stdafx.h#include PipeServer.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ The one and only application objectCWinApp theApp;usin
3、g namespace std;int _tmain(int argc, TCHAR* argv, TCHAR* envp)int nRetCode = 0;int err;BOOL rc;HANDLE hPipeHandle1;char lpName=.pipemyPipe;char InBuffer50=;char OutBuffer50=;DWORD BytesRead,BytesWrite;hPipeHandle1=CreateNamedPipe( (LPCTSTR)lpName, PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED|WRITE_DAC, P
4、IPE_TYPE_MESSAGE|PIPE_READMODE_BYTE|PIPE_WAIT, 1,20,30,NMPWAIT_USE_DEFAULT_WAIT, (LPSECURITY_ATTRIBUTES)NULL ); if(hPipeHandle1=INVALID_HANDLE_VALUE)|(hPipeHandle1=NULL) err=GetLastError();printf(Server Pipe Creat Fail!err=%dn,err);exit(1); else printf(Server Pipe Creat Success!n); while(1) rc=Conne
5、ctNamedPipe(hPipeHandle1,(LPOVERLAPPED)NULL);if(rc=0)err=GetLastError();printf(Server Pipe Connect Fail err=%dn,err);exit(2);else printf(Server Pipe Connect Successn);strcpy(InBuffer,);strcpy(OutBuffer,);rc=ReadFile(hPipeHandle1,InBuffer,sizeof(InBuffer),&BytesRead, (LPOVERLAPPED)NULL);if(rc=0&Bytes
6、Read=0)err=GetLastError();printf(Server Read Pipe Fail err=%dn,err);exit(3);else printf(Server Read Pipe Success!nDATA from Client is=%sn,InBuffer);rc=strcmp(InBuffer,end);if(rc=0) printf(Server Write Pipe Fail!n);else printf(Server Write Pipe Success!n);DisconnectNamedPipe(hPipeHandle1);rc=strcmp(O
7、utBuffer,end);if(rc=0) break; printf(Now Server be END!n); CloseHandle(hPipeHandle1);/*/ initialize MFC and print and error on failureif (!AfxWinInit(:GetModuleHandle(NULL), NULL, :GetCommandLine(), 0)/ TODO: change error code to suit your needscerr _T(Fatal Error: MFC initialization failed) endl;nR
8、etCode = 1;else/ TODO: code your applications behavior here.CString strHello;strHello.LoadString(IDS_HELLO);cout (LPCTSTR)strHello endl;*/return nRetCode;客户端/PipeClient.cpp:Definestheentrypointfortheconsoleapplication./#includestdafx.h#includePipeClient.h#ifdef_DEBUG#definenewDEBUG_NEW#undefTHIS_FIL
9、EstaticcharTHIS_FILE=_FILE_;#endif/TheoneandonlyapplicationobjectCWinApptheApp;usingnamespacestd;int_tmain(intargc,TCHAR*argv,TCHAR*envp)BOOLrc=0;charlpName=.pipemyPipe;charInBuffer50=;charOutBuffer50=;DWORDBytesRead;intnRetCode=0;interr=0;while(1)strcpy(InBuffer,);strcpy(OutBuffer,);printf(InputDat
10、aPlease!n);scanf(%s,InBuffer);rc=strcmp(InBuffer,end);/initializeMFCandprintanderroronfailureif(rc=0)rc=CallNamedPipe(lpName,InBuffer,sizeof(InBuffer),OutBuffer,sizeof(OutBuffer),&BytesRead,NMPWAIT_USE_DEFAULT_WAIT);break;rc=WaitNamedPipe(lpName,NMPWAIT_WAIT_FOREVER);if(rc=0)err=GetLastError();print
11、f(WaitPipeFail!err=%dn,err);exit(1);elseprintf(WaitPipeSuccess!n);rc=CallNamedPipe(lpName,InBuffer,sizeof(InBuffer),OutBuffer,sizeof(OutBuffer),&BytesRead,NMPWAIT_USE_DEFAULT_WAIT);rc=strcmp(OutBuffer,end);if(rc=0)break;if(rc=0)err=GetLastError();printf(PipeCallFail!err=%dn,err);exit(1);elseprintf(PipeCallSuccess!nDatafromServeris%sn,OutBuffer);printf(NowClienttobeEnd!n);returnnRetCode;五、实验结论Windows进程通信工具有管道Pipe、共享内存、文件映射等,其中管道通信是拥有两个端点通信句柄的进程间的通信方式。分为单向和双向。单向是指一个端点写,另一个端点读。 管道分为匿名管道和命名管道。匿名管道适用于父进程和子进程、两个子进程之间。匿名管道不能在网上和不相关进程间使用。命名管道适用于不相关的进程和不同计算机之间。命名管道在网络上同时和多个管道通信会困难许多。专心-专注-专业
限制150内