How can I implement this using a thread?
After establishing connection between the server and client, either client or server can start sending messages. What i am implementing is first the client starts, then only server. I need some help with this part.
I have done following implementations for pipe communications:
1.Serverside
#include "stdafx.h"
#include "windows.h"
#define g_szPipeName "\\\\.\\Pipe\\MyNamedPipe"
#define BUFFER_SIZE 1024
int main()
{
HANDLE hPipe;
char szBuffer[BUFFER_SIZE];
DWORD cbBytes;
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 (hPipe==INVALID_HANDLE_VALUE )
{
printf("\nError occurred while creating the pipe: %d", GetLastError());
return 1;
}
else
{
printf("\nNamed Pipe Created and Server Started.\n");
}
printf("\nWaiting for client connection......\n");
BOOL bClientConnected = ConnectNamedPipe(hPipe, NULL);
if ( bClientConnected==FALSE)
{
printf("\nError occurred while connecting to the client: %d", GetLastError());
CloseHandle(hPipe);
return 1;
}
else
{
printf("\nConnection Established between Client and Server\n");
}
BOOL bResult = ReadFile(hPipe,szBuffer,sizeof(szBuffer),&cbBytes,NULL);
if ( (!bResult) || (cbBytes==0 ))
{
printf("\nError occurred while reading from the client: %d", GetLastError());
CloseHandle(hPipe);
return 1;
}
else
{
printf("\nReading from the Client....\n");
}
printf("\nClient sent the following message: %s", szBuffer);
printf("\n");
printf("\nEnter Message to be sent to Client\n");
gets(szBuffer);
bResult = WriteFile(hPipe,szBuffer,strlen(szBuffer)+1,&cbBytes,NULL);
if ( (!bResult) || (strlen(szBuffer)+1 != cbBytes))
{
printf("\nError occurred while writing to the client: %d", GetLastError());
CloseHandle(hPipe);
return 1;
}
else
{
printf("\nWriting to client......\n");
}
CloseHandle(hPipe);
return 0;
}
2.ClientSide
#include "stdafx.h"
#include "windows.h"
#define g_szPipeName "\\\\.\\Pipe\\MyNamedPipe"
#define BUFFER_SIZE 1024
int main()
{
HANDLE hPipe;
char szBuffer[BUFFER_SIZE];
DWORD cbBytes;
hPipe = CreateFile(g_szPipeName,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if (hPipe ==INVALID_HANDLE_VALUE )
{
printf("\nError occurred while connecting to the server: %d", GetLastError());
return 1;
}
else
{
printf("\nClient Started.");
printf("reading.....\n");
BOOL bResult = ReadFile(hPipe,szBuffer,sizeof(szBuffer),&cbBytes,NULL);
printf("Message from Server:%s\n",szBuffer);
}
printf("\nEnter a message to be sent to the server: ");
gets(szBuffer);
BOOL bResult = WriteFile(hPipe,szBuffer,strlen(szBuffer)+1,&cbBytes,NULL);
if ( (!bResult) || (strlen(szBuffer)+1 != cbBytes))
{
printf("\nError occurred while writing to the server: %d", GetLastError());
CloseHandle(hPipe);
return 1;
}
else
{
printf("\nWriting to Server successful...");
}
bResult = ReadFile(hPipe,szBuffer,sizeof(szBuffer),&cbBytes,NULL);
if ( (!bResult) || (cbBytes ==0))
{
printf("\nError occurred while reading from the server: %d", GetLastError());
CloseHandle(hPipe);
return 1;
}
else
{
printf("\nReading from Server....\n");
}
printf("\nServer sent the following message: %s", szBuffer);
printf("\n");
CloseHandle(hPipe);
return 0;
}