问题描述
我试图使用Win32 API在Visual Studio 2019中用C ++编写服务器/客户端程序。
这是相关文档:Named Pipe Open Modes
我使用了4个API:
在服务器端(创建管道并对其进行写操作的一方):CreateNamedPipe()
,WriteFile()
在客户端(通过管道进行连接和读取):CreateFile()
,ReadFile()
但是,我发现服务器无法写入管道。
以下是我使用的代码。
Servermain.cpp
#include <iostream>
#include <windows.h>
using namespace std;
void namedPipeServer()
{
HANDLE hPipeServer;
char Wbuffer[1024] = "Hello,from the pipe server!";
DWORD dwWrite;
BOOL writeSuccessFlag;
//Create a named pipe
hPipeServer = CreateNamedPipe(
TEXT("\\\\.\\pipe\\Agentpipe"),//lpName
PIPE_ACCESS_OUTBOUND,//dwOpenMode
PIPE_TYPE_BYTE,//dwPipeMode
1,//nMaxInstances
1024 * 16,//nOutBufferSize
1024 * 16,//nInBufferSize
NMPWAIT_USE_DEFAULT_WAIT,//nDefaultTimeOut
NULL); //lpSecurityAttributes
cout << "Inside namedPipeServer()" << endl;
if (hPipeServer != INVALID_HANDLE_VALUE)
{
cout << "Just writing to pipe" << endl;
writeSuccessFlag = WriteFile(
hPipeServer,//HANDLE hFile
Wbuffer,//LPCVOID lpBuffer
30,//DWORD nNumberOfBytesToWrite
&dwWrite,NULL //LPOVERLAPPED lpOverlapped
);
if (writeSuccessFlag)
{
cout << "Server has written to pipe!" << endl;
}
else
{
cout << "Unsuccessful write to pipe,From Agent" << endl;
}
}
else
{
cout << "Unsuccesful pipe connection. hPipeServer: " << hPipeServer << endl;
}
}
int main()
{
cout << "Inside Agent server. Creating a named pipe.\n" << endl;
namedPipeServer();
while (1);
return 0;
}
Clientmain.cpp:
#include <iostream>
#include <windows.h>
using namespace std;
void readFromPipe()
{
HANDLE hPipeClient;
char rBuffer[1024];
DWORD dwRead;
BOOL readSuccessFlag = 0;
//Connect to the server pipe: \\.\\pipe\\Agentpipe
cout << "Inside readFromPipe()." << endl;
hPipeClient = CreateFile(
TEXT("\\\\.\\pipe\\Agentpipe"),//lpFileName
GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,NULL
);
while (hPipeClient != INVALID_HANDLE_VALUE)
{
cout << "Just connecting to pipe" << endl;
readSuccessFlag = ReadFile(
hPipeClient,//HANDLE hFile,rBuffer,//LPVOID lpBuffer,30,//DWORD nNumberOfBytesToRead,&dwRead,//LPDWORD lpNumberOfBytesRead,NULL //LPOVERLAPPED lpOverlapped
);
if (readSuccessFlag)
{
cout << "Client has read from pipe of Agent!" << endl;
cout << "From Agent Pipe: " << rBuffer << endl;
}
else
{
cout << "Unsuccessful Pipe read!" << endl;
}
}
if(hPipeClient == INVALID_HANDLE_VALUE)
{
cout << "Unsuccesful pipe connection at client end. hPipeClient: " << hPipeClient << endl;
}
}
int main()
{
cout << "Inside the client. Calling readFromPipe()" << endl;
readFromPipe();
while (1);
return 0;
}
执行上述程序后,表明服务器无法写入管道,并且服务器端的输出为:
Inside Agent server. Creating a named pipe.
Inside namedPipeServer()
Just writing to pipe
Unsuccessful write to pipe,From Agent
客户端控制台上的输出为:
Inside the client. Calling readFromPipe()
Inside readFromPipe().
Just connecting to pipe
在查看Win32文档中的示例程序时,我发现这些Win32 API的使用顺序不同,如下所示:
Pipe Server程序:
main(){
...
namedPipeServer()
...
}
void namedPipeServer()
{
...
CreateFile()
WriteFile()
...
}
Pipe Client程序:
main(){
...
readFromPipe()
...
}
void readFromPipe()
{
...
CreateNamedPipe()
ReadFile()
...
}
如果有人可以特别明确地说明我对CreateNamedPipe()
和CreateFile()
的使用,我将感到高兴。
服务器是否必须首先使用CreateFile()
(在创建管道之前,先将其写入),还是可以使用CreateNamedPipe()
?
在 MY 程序中使用API的顺序发布不正确吗?如果是这样,请说明原因。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)