命名管道通信

SKATE11 2013-08-17 07:18:19
大家看下我这代码为什么不能在两台机器上通信 错误提示为 拒绝访问
服务器端:
#include<iostream>
#include<windows.h>
#include<string.h>
using namespace std;

int main( void )
{
HANDLE pipehandle;
char buf[ 256 ];
DWORD bytesRead;
if( ( pipehandle = CreateNamedPipe( "\\\\.\\pipe\\cao", PIPE_ACCESS_DUPLEX , PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, 0, 0, 5000, NULL ) ) == INVALID_HANDLE_VALUE )
{
printf( "CreateNamedPipe failed with error %d/n", GetLastError() );
system( "pause" );
return 0;
}
printf( "server is running\n" );
if( ConnectNamedPipe( pipehandle, NULL ) == 0 )
{
printf( "connectNamedPipe failed with error %d\n", GetLastError() );
CloseHandle( pipehandle );
system( "pause" );
return 0;
}
if( ReadFile( pipehandle, buf, strlen( buf ), &bytesRead, NULL ) == 0 )
{
printf( "ReadFile failed with error %d/n", GetLastError() );
CloseHandle( pipehandle );
system( "pause" );
return 0;
}
//buf[bytesRead] = '\0';
printf( "%s\n", buf );
if ( DisconnectNamedPipe( pipehandle ) == 0 )
{
printf( "DisconnectNamedPipe failed with error %d\n", GetLastError() );
CloseHandle( pipehandle );
system( "pause" );
return 0;
}
system( "pause" );
return 0;
}
客户端:
#include<iostream>
#include<windows.h>
#include<string.h>
using namespace std;
int main( void )
{
HANDLE pipehandle;
DWORD writesbytes;
char buff[ 256 ];
if( WaitNamedPipe("\\\\apical-3bbb979\\pipe\\cao", NMPWAIT_WAIT_FOREVER ) == 0 )
{
printf( "WaitNamedPipe failed with error %d\n", GetLastError() );
system( "pause" );
return 0;
}
else
{
printf("connect .......\n");
}
if( ( pipehandle = CreateFile( "\\\\apical-3bbb979\\pipe\\cao", GENERIC_READ | GENERIC_WRITE, 0, ( LPSECURITY_ATTRIBUTES )NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, ( HANDLE )NULL ) ) == INVALID_HANDLE_VALUE )
{
printf( "CreateFile failed with error %d\n", GetLastError() );
system( "pause" );
return 0;
}
else
{
printf("opened pleass input num\n");

}
ZeroMemory( &buff, sizeof( buff ) );
gets( buff );
if( WriteFile( pipehandle, buff, strlen( buff ) + 1, &writesbytes, NULL ) == 0 )
{
printf( "WriteFile failed with error %d\n", GetLastError() );
CloseHandle( pipehandle );
system( "pause" );
return 0;
}
printf( "write %d bytes", writesbytes );
CloseHandle( pipehandle );
system( "pause" );
return 0;
}

apical-3bbb979是我的计算机名
在本机可以 在局域网上两台机器上就不行了 可以监听到 但是不能打开管道
...全文
108 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
woshinia 2013-08-19
  • 打赏
  • 举报
回复
用之前设置一下命名管道的使用权限。
gykgod 2013-08-19
  • 打赏
  • 举报
回复
权限不够吧 看看这个帖子 http://bbs.csdn.net/topics/310203566

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧