#include <iostream>
#include <tchar.h>
#include <string>
#include <Windows.h>
#include <cassert>
#include "memory_leak.h"
using namespace std;
template<typename TYPE>
bool read(HANDLE hPipe,TYPE &data);
bool readString(HANDLE hPipe,string &str);
bool readData(HANDLE hPipe);
template<typename TYPE>
bool write(HANDLE hPipe,TYPE &data);
bool writeString(HANDLE hPipe,const char *str);
/**
* The MOD's trajectory point
*/
struct TrjPoint
{
time_t t; ///<The time
double x; ///<The latitude
double y; ///<The longtitude
float v; //<The velocity
int d; //<The direction
};
/**
* Detect memory leak on exit
*/
void Exit()
{
int i = _CrtDumpMemoryLeaks();
assert( i == 0);
}
int main(int argc,char *argv[])
{
int maxConn=1;
bool ret;
_TCHAR *pipeName=_T("\\\\.\\Pipe\\IPC");
HANDLE hPipe,hEvent,hThread;
hPipe=CreateNamedPipe(pipeName,
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE|PIPE_READMODE_BYTE|PIPE_WAIT,
10,
128,
128,
150,
NULL);
if(hPipe==INVALID_HANDLE_VALUE)
{
cout<<"Unable to create the pipe"<<endl;
return 1;
}
while(true)
{
ret=ConnectNamedPipe(hPipe,NULL);
if(ret)
{
string str="";
do
{
readString(hPipe,str);
if(str.length()>0)
{
cout<<str<<endl;
writeString(hPipe,str.c_str());
}
}while(str!="quit");
cout<<"Connection finished!"<<endl;
}
DisconnectNamedPipe(hPipe);
}
getchar();
return 0;
}
template <typename TYPE>
bool read(HANDLE hPipe,TYPE &data)
{
DWORD size=sizeof(TYPE),dwBytesRead=0;
bool ret=ReadFile(hPipe,&data,size,&dwBytesRead,NULL);
return ret;
}
bool readString(HANDLE hPipe,string &str)
{
DWORD size,dwBytesRead=0;
bool ret=read(hPipe,(int &)size);
if(ret&&size>0)
{
_TCHAR *c=new char[size+1];
ret=ReadFile(hPipe,c,size,&dwBytesRead,NULL);
c[size]='\0';
str= string(c);
}
return ret;
}
bool readData(HANDLE hPipe)
{
int i=0;
string str;
bool ret=read(hPipe,i);
if(ret)
{
ret=readString(hPipe,str);
if(ret)
{
cout<<i<<":"<<str<<endl;
}
}
return ret;
}
///<summary>
/// Read the data with type TYPE
///</summary>
template<typename TYPE>
bool write(HANDLE hPipe,TYPE &data)
{
DWORD size=sizeof(TYPE),dwBytesWritten=0;
bool ret=WriteFile(hPipe,&data,size,&dwBytesWritten,NULL);
return ret;
}
///<summary>
/// Write a string to the named pipe
///1. write the length of the string
///2. write the string by character sequence
///</summary>
bool writeString(HANDLE hPipe,const char *str)
{
DWORD dwBytesWritten=0;
DWORD size=strlen(str)+1;
bool ret=true;
if(size>0)
{
ret=write(hPipe,(int &)size);
if( ret)
{
ret=WriteFile(hPipe,str,
size,&dwBytesWritten,NULL);
}
}
return ret;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
IPCDemo.rar (23个子文件)
IPCDemo
IPCDemo
memory_leak.h 318B
main.cpp 3KB
IPCDemo.vcxproj 4KB
IPCDemo.vcxproj.filters 1KB
IPCDemo.vcxproj.user 143B
IPCDemo.suo 41KB
IPCDemo.sln 3KB
IPCClient
Debug
IPCClient.Build.CppClean.log 2KB
IPCClient.log 419B
memory_leak.h 318B
IPCClient.vcxproj.filters 1KB
main.cpp 2KB
IPCClient.vcxproj.user 143B
IPCClient.vcxproj 4KB
IPC
NamedPipe
NamedPipeClient.cs 777B
NamedPipe.cs 8KB
NamedPipeServer.cs 654B
Program.cs 904B
IPC.csproj 3KB
Properties
AssemblyInfo.cs 1KB
obj
x86
Debug
TempPE
DesignTimeResolveAssemblyReferencesInput.cache 6KB
bin
Debug
IPC.vshost.exe 11KB
Release
ObjectToByteArray.cs 877B
共 23 条
- 1
yaguang_li
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页