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);
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);
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;
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;
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;
}