Click here to Skip to main content
15,920,031 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I created one server application and one client application, while i'm sending the message form client to server i'm getting "Error occurred while reading message 109" is displaying,

What I have tried:

here, i tried my server and client code is below,

server code:-


#include "stdafx.h"
#include<Windows.h>
#include<iostream>
using namespace std;

#define g_szPipeName "\\\\.\\pipe\\pipename"
#define BUFFER_SIZE 500
#define ACK_MESSAGE_RECV "Message received sucessfully"

HANDLE hPipe;

class NamedPipeServerApplication
{
public:
	int repeat()
	{
		char szBuffer[BUFFER_SIZE];
		DWORD cbBytes;

		BOOL bResult = ReadFile(hPipe,szBuffer,sizeof(szBuffer),&cbBytes,NULL);
		if((bResult) || (0==cbBytes))
		{
			cout<<"Error occured while reading from the client"<<GetLastError()<<endl;
			CloseHandle(hPipe);
			//system("pause");
			return 1;

		}
		else 
		{
			cout<<"ReadFile  was sucessful"<<endl;
		}
		cout<<"Client sent the following message :"<<szBuffer<<endl;
		strcpy(szBuffer,ACK_MESSAGE_RECV);
		bResult = WriteFile(hPipe,szBuffer,strlen(szBuffer),&cbBytes,NULL); 

		if((!bResult) || strlen(szBuffer) != cbBytes)
		{
			cout<<"Error occured while writing the client code"<<GetLastError()<<endl;
			CloseHandle(hPipe);
			//system("pause");
			return 1;
		}
		else
		{
			cout<<"WriteFile was sucessful"<<endl;
		}
		repeat();
	}
};
int _tmain(int argc, _TCHAR* argv[])
{
	NamedPipeServerApplication Server;

	hPipe = CreateNamedPipe(g_szPipeName,PIPE_ACCESS_DUPLEX,PIPE_TYPE_MESSAGE |
						PIPE_READMODE_MESSAGE | PIPE_WAIT,PIPE_UNLIMITED_INSTANCES,
						BUFFER_SIZE,BUFFER_SIZE,NMPWAIT_USE_DEFAULT_WAIT,NULL);

	if(INVALID_HANDLE_VALUE == hPipe)
	{
		cout<<"Error occured while creating the pipe"<<GetLastError()<<endl;
		//system("pause");
		return 1;
	}
	else
	{
		cout<<"CreateNamedPipe() was sucessful"<<endl;
	}
	cout<<"waiting for the client connection...."<<endl;
	BOOL bClientConnected = ConnectNamedPipe(hPipe,NULL);

	if(FALSE== bClientConnected)
	{
		cout<<"Error occured while connecting to the client "<<GetLastError()<<endl;
		//system("pause");
		return 1;
	}
	else
	{
		cout<<"ConnectNamePipe() was sucessfull"<<endl;
	}

	Server.repeat();
	return 0;
}


and My client code is :-

#include "stdafx.h"
#include<iostream>
#include<Windows.h>
using namespace std;

#define g_szPipeName "\\\\.\\pipe\\pipename"

#define BUFFSIZE 500
#define ACK_MESG_RECV  "Message received sussefully"

HANDLE hPipe;
BOOL bResult;

class NamedPipeClient
{
public:
	
	int repeat()
	{
		char szBuffer[BUFFSIZE];
		cout<<"\nEnter a message to be sent to the server: ";
		gets(szBuffer);
		
		DWORD cbBytes;
		BOOL bResult = WriteFile(hPipe,szBuffer,strlen(szBuffer),&cbBytes,NULL);    
	
	if ((!bResult) || (strlen(szBuffer) != cbBytes))
		{
		cout<<"\nError occurred while writing to the server:"<< GetLastError()<<endl;
		CloseHandle(hPipe);
		system("Pause");
		return 1; 
		}
		else
			{
			cout<<"\nWriteFile was successful."<<endl;
			}
			bResult = ReadFile(hPipe,szBuffer,sizeof(szBuffer),&cbBytes,NULL);  

		if ((!bResult) || (0 == cbBytes))
		{
			cout<<"\nError occurred while reading from the server: "<<GetLastError()<<endl;
			CloseHandle(hPipe);
			system("Pause");
			return 1; 
		}
		else
			{
				cout<<"\nReadFile was successful."<<endl;
			}
				cout<<"\nServer sent the following message: %s"<<szBuffer<<endl;
				repeat();
	}
		

};


int _tmain(int argc, _TCHAR* argv[])
{
	NamedPipeClient Client;

	hPipe = CreateFile( 
          g_szPipeName,  
          GENERIC_READ |			 
          GENERIC_WRITE, 
          0,              
          NULL,           
          OPEN_EXISTING,   
          0,              
          NULL);

	 if (INVALID_HANDLE_VALUE == hPipe) 
     {
          cout<<"\nError occurred while connecting to the server: "<<GetLastError(); 
         
		  system("Pause");
          return 1;
     }
     else
     {
          cout<<"\nCreateFile was successful."<<endl;
     }
	Client.repeat(); 
	return 0;
}
Posted
Updated 18-Feb-19 6:25am
Comments
[no name] 18-Feb-19 3:18am    
Last Error 109:
System Error Codes (0-499) - Windows applications | Microsoft Docs[^]
-> ERROR_BROKEN_PIPE 109 (0x6D) The pipe has been ended.
Rick York 18-Feb-19 12:31pm    
Using a global handle to the pipe is a really bad idea. At a minimum, it should be an argument passed to the repeat method. Most people would make it a member of the class and put the call to CreateFile in the class also.

1 solution

You need to use the debugger to see what is going on and read some documentation. There you would read about the error message.

The art of programming is to deal with errors gracefully and re-establish the connection. You need also to fix the server code which also needs to delete the pipe and so can reconnect.

Consider to use some socket programming like decribed in the article Programming Windows TCP Sockets in C++ for the Beginner,
 
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