Click here to Skip to main content
15,911,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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";
       
    }
 }


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;
}
Posted
Updated 23-Feb-19 4:19am
v2
Comments
Richard MacCutchan 23-Feb-19 6:55am    
I cannot see how that code could even compile. Your function definitions are missing the names of all the parameters.

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.
 
Share this answer
 
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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900