命名管道客户端错误5 (c++)
Named Pipes Client Error 5 (C++)
好的,下面我有命名管道服务器/客户端的代码。我把服务器放在我的Windows 8电脑上,它创建了一个管道,然后等待客户端连接。然后我在Windows 7上启动客户端,但它返回错误5(访问被拒绝,我想??)有人能告诉我为什么它给我错误5吗?
提前感谢所有的答案
服务器代码#include "stdafx.h"
#include "windows.h"
#include <iostream>
using namespace std;
#define g_szPipeName "\\.\pipe\pipename" //Name given to the pipe
#define BUFFER_SIZE 1024 //1k
#define ACK_MESG_RECV "Message received successfully"
#define _CRT_SECURE_NO_WARNINGS
HANDLE hPipe;
int repeate() {
char szBuffer[BUFFER_SIZE];
DWORD cbBytes;
//We are connected to the client.
//To communicate with the client we will use ReadFile()/WriteFile()
//on the pipe handle - hPipe
//Read client message
BOOL bResult = ReadFile(
hPipe, // handle to pipe
szBuffer, // buffer to receive data
sizeof(szBuffer), // size of buffer
&cbBytes, // number of bytes read
NULL); // not overlapped I/O
if ((!bResult) || (0 == cbBytes))
{
printf("nError occurred while reading from the client: %d", GetLastError());
CloseHandle(hPipe);
system("Pause");
return 1; //Error
}
else
{
printf("nReadFile() was successful.");
}
printf("nClient sent the following message: %s", szBuffer);
strcpy(szBuffer, ACK_MESG_RECV);
//Reply to client
bResult = WriteFile(
hPipe, // handle to pipe
szBuffer, // buffer to write from
strlen(szBuffer) + 1, // number of bytes to write, include the NULL
&cbBytes, // number of bytes written
NULL); // not overlapped I/O
if ((!bResult) || (strlen(szBuffer) + 1 != cbBytes))
{
printf("nError occurred while writing to the client: %d", GetLastError());
CloseHandle(hPipe);
system("Pause");
return 1; //Error
}
else
{
printf("nWriteFile() was successful.");
}
repeate();
}
int main(int argc, char* argv[])
{
SECURITY_DESCRIPTOR sd;
InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = &sd;
hPipe = CreateNamedPipe(
g_szPipeName, // pipe name
PIPE_ACCESS_DUPLEX, // read/write access
PIPE_TYPE_MESSAGE | // message type pipe
PIPE_READMODE_MESSAGE | // message-read mode
PIPE_WAIT, // blocking mode
PIPE_UNLIMITED_INSTANCES, // max. instances
BUFFER_SIZE, // output buffer size
BUFFER_SIZE, // input buffer size
NMPWAIT_USE_DEFAULT_WAIT,
&sa);
if (INVALID_HANDLE_VALUE == hPipe)
{
printf("nError occurred while creating the pipe: %d", GetLastError());
system("Pause");
return 1; //Error
}
else
{
printf("nCreateNamedPipe() was successful.");
}
printf("nWaiting for client connection...");
//Wait for the client to connect
BOOL bClientConnected = ConnectNamedPipe(hPipe, NULL);
if (FALSE == bClientConnected)
{
printf("nError occurred while connecting to the client: %d", GetLastError());
CloseHandle(hPipe);
system("Pause");
return 1; //Error
}
else
{
printf("nConnectNamedPipe() was successful.");
}
repeate();
}
客户机代码#include "stdafx.h"
#include "windows.h"
#include <iostream>
#define g_szPipeName "\\MyComputerName\pipe\pipename" //Name given to the pipe
#define BUFFER_SIZE 1024 //1k
#define ACK_MESG_RECV "Message received successfully"
HANDLE hPipe;
int repeate() {
char szBuffer[BUFFER_SIZE];
printf("nEnter a message to be sent to the server: ");
gets(szBuffer);
DWORD cbBytes;
//Send the message to server
BOOL bResult = WriteFile(
hPipe, // handle to pipe
szBuffer, // buffer to write from
strlen(szBuffer) + 1, // number of bytes to write, include the NULL
&cbBytes, // number of bytes written
NULL); // not overlapped I/O
if ((!bResult) || (strlen(szBuffer) + 1 != cbBytes))
{
printf("nError occurred while writing to the server: %d", GetLastError());
CloseHandle(hPipe);
system("Pause");
return 1; //Error
}
else
{
printf("nWriteFile() was successful.");
}
//Read server response
bResult = ReadFile(
hPipe, // handle to pipe
szBuffer, // buffer to receive data
sizeof(szBuffer), // size of buffer
&cbBytes, // number of bytes read
NULL); // not overlapped I/O
if ((!bResult) || (0 == cbBytes))
{
printf("nError occurred while reading from the server: %d", GetLastError());
CloseHandle(hPipe);
system("Pause");
return 1; //Error
}
else
{
printf("nReadFile() was successful.");
}
printf("nServer sent the following message: %s", szBuffer);
repeate();
}
int main(int argc, char* argv[])
{
//Connect to the server pipe using CreateFile()
hPipe = CreateFile(
g_szPipeName, // pipe name
GENERIC_READ | // read and write access
GENERIC_WRITE,
0, // no sharing
NULL, // default security attributes
OPEN_EXISTING, // opens existing pipe
0, // default attributes
NULL); // no template file
if (INVALID_HANDLE_VALUE == hPipe)
{
printf("nError occurred while connecting to the server: %d", GetLastError());
//One might want to check whether the server pipe is busy
//This sample will error out if the server pipe is busy
//Read on ERROR_PIPE_BUSY and WaitNamedPipe() for that
system("Pause");
return 1; //Error
}
else
{
printf("nCreateFile() was successful.");
}
//We are done connecting to the server pipe,
//we can start communicating with the server using ReadFile()/WriteFile()
//on handle - hPipe
repeate();
}
通过添加ip地址,子网掩码,默认网关来配置两台计算机。从网络和共享中心,打开所有共享和关闭密码保护共享。测试它ping ip地址从cmd。两台计算机的用户帐户不应设置密码保护。这就是为什么我修复了客户端错误5,并在两台远程计算机之间设置了命名管道通信
客户端应该使用OpenFile(),而不是CreateFile()。客户端不想创建任何新的东西,它想要与现有的管道通信。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'