如何使用namedpipe在客户端和服务器之间发送和读取消息 [英] How to send and read message between client and serve using namedpipe
问题描述
I have created Client and Server Application, and when I am sending message from client to server, server is reading the message from client but it is displaying junk messages, how can I display exact message?
can you please explain.
我尝试了什么:
我的服务器代码是: -
What I have tried:
My Server code is:-
Server::Server(void)
{
hPipe = CreateNamedPipe("\\\\.\\pipe\\mypipe",PIPE_ACCESS_DUPLEX,PIPE_TYPE_MESSAGE|
PIPE_READMODE_MESSAGE|PIPE_WAIT,PIPE_UNLIMITED_INSTANCES,BUFSIZE,BUFSIZE,0,NULL);
if(hPipe==INVALID_HANDLE_VALUE)
{
std::cout<<"PipeCreation Failed";
}
else
std::cout<<"PipeCreation Sucessful";
Run();
}
void Server::Run()
{
std::cout<<"Server running:\n";
std::cout<<"waiting for the client:\n";
ConnectToClient(hPipe,NULL);
ReadFile(hPipe,m_buffer,127 * sizeof(wchar_t),cbRead,NULL);
}
bool Server::ReadFile(HANDLE,LPVOID,DWORD,LPDWORD,LPOVERLAPPED)
{
wchar_t m_buffer[128];
DWORD cbRead=0;
if (result) {
m_buffer[sizeof(wchar_t)] = '\0';
std::cout << "Number of bytes read: " << cbRead <<std::endl;
std::cout << "Message: " << m_buffer << std::endl;
} else {
std::cout << "Failed to read data from the pipe." << std::endl;
}
return true;
}
void Server::ConnectToClient(HANDLE,LPOVERLAPPED)
{
if(FALSE == ConnectNamedPipe(hPipe, NULL))
{
std::cout<<"Pipe connection failed";
}
else
{
std::cout<<"Pipe connection sucessful";
}
}
我的客户代码是: -
客户::客户(无效)
{
hPipe = CreateFile(\\\\.\\pipe \\ mypipe,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if(hPipe == INVALID_HANDLE_VALUE)
{
std :: cout<<管道连接失败:\ n; < br $>
}
其他
std :: cout<<管道连接成功:\ n;
WriteFile(hPipe,m_buffer,cbToWrite,cbWritten,NULL);
}
bool Client :: WriteFile(HANDLE,LPCVOID,DWORD, LPDWORD,LPOVERLAPPED)
{
const wchar_t * m_buffer = L*** Hello Pipe World ***;
if(result){
std :: cout<< 发送的字节数:<< cbWritten<< std :: endl;
} else {
std :: cout<< 消息:<< std :: endl;
}
返回true;
}
My Client code is :-
Client::Client(void)
{
hPipe = CreateFile("\\\\.\\pipe\\mypipe",GENERIC_READ |GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if(hPipe == INVALID_HANDLE_VALUE)
{
std::cout<<"pipe connection Failed:\n";
}
else
std::cout<<"pipe connected sucessfully:\n";
WriteFile(hPipe,m_buffer,cbToWrite,cbWritten,NULL);
}
bool Client::WriteFile(HANDLE,LPCVOID,DWORD,LPDWORD,LPOVERLAPPED)
{
const wchar_t *m_buffer = L"*** Hello Pipe World ***";
if (result) {
std::cout << "Number of bytes sent: " << cbWritten << std::endl;
} else {
std::cout << "Message:" << std::endl;
}
return true;
}
推荐答案
代码示例由 MSDN
轻轻提供(例如,参见命名管道客户端 - Windows应用程序| Microsoft Docs [ ^ ])通常效果很好。我建议你使用这样的程序作为应用程序的起点(工作)点。
The code samples gently provided by MSDN
(see, for instance Named Pipe Client - Windows applications | Microsoft Docs[^]) usually works well. I suggest you to use such programs as starting (working) points for your application.
这是一篇很有趣的文章,它很好地解释了命名管道,并有一个示例代码的链接:使用命名管道将GUI连接到Windows中的控制台应用程序Dobb博士 [ ^ ]
Here is an interesting article that explains named pipes well, and has a link to the example code: Using Named Pipes to Connect a GUI to a Console App in Windows | Dr Dobb's[^]
这篇关于如何使用namedpipe在客户端和服务器之间发送和读取消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!