OK
I now have my two applications running in synchro, however I only have them working in one direction
After VB debugging the Window will now open and announce that it is waiting for a connection, I then debug the second .cpp file and that connects to server fine. I can then send messages to the named server from the client and they will display correctly, but cannot send them back to the client i.e. so as to have a working 2-way chat application!!!
I have tried various diffrent bits of coding but I am cautious as what I have just now is working quite well.
Here are the 2 pieces of code in C++.................
This is the namedServer code:
#include <windows.h>
#include <iostream>
#include <assert.h>
using namespace std;
void main()
{
cout << "Chat Application 1 is Running. Awaiting Client Connection...." << endl;
//*********************
//create the named pipe
//*********************
HANDLE hNamedPipe = CreateNamedPipe( "\\\\.\\pipe\\chatapppipe",
PIPE_ACCESS_INBOUND,
0,
1, // max instances
0,
0,
0, // timeout in milliseconds
0);
assert(hNamedPipe != INVALID_HANDLE_VALUE);
//******************************************
//block until the client connects
//and indicate when the client has connected
//******************************************
BOOL bStatus = ConnectNamedPipe(hNamedPipe, 0);
assert(bStatus != 0);
cout << "New Chat Client has Joined" << endl;
//********************************************************
//loop and read what is passed by the client over the pipe
//until the client sends the message 'Goodbye'
//********************************************************
DWORD dwNumberOfBytesRead;
char clientMessage[80];
do
{
//read the message
bStatus = ReadFile( hNamedPipe,
clientMessage,
79,
&dwNumberOfBytesRead,
0);
assert(bStatus!=0);
//add zero termination to the client message string
clientMessage[dwNumberOfBytesRead] = '\0';
//output the message
cout << "Chat User 2 Says: " <<
clientMessage <<
endl;
}
while (strcmp("Goodbye",clientMessage) != 0);
//*****************************
//disconnect and close the pipe
//*****************************
bStatus = DisconnectNamedPipe(hNamedPipe);
assert(bStatus != 0);
CloseHandle(hNamedPipe);
cout << "This Chat Session is Finished" << endl;
} //end of main
This is the NamedClient code:
#include <windows.h>
#include <iostream>
#include <assert.h>
using namespace std;
void main()
{
cout << "Client Chat Application has connected to the Server" << endl;
//*******************
//open the named pipe
//*******************
HANDLE hNamedPipe = CreateFile( "\\\\.\\pipe\\chatapppipe",
GENERIC_WRITE,
0, // no sharing
0, // default security
OPEN_EXISTING,
0,
0);
assert(hNamedPipe != INVALID_HANDLE_VALUE);
//***************************************************
//loop until the user types the string 'Goodbye'
//sending all that the user types to the server using
//the named pipe
//***************************************************
DWORD dwNumberOfBytesWritten;
BOOL bStatus;
char clientMessage[80];
do
{
cin.getline (clientMessage, 80, '\n');
bStatus = WriteFile( hNamedPipe,
clientMessage,
strlen(clientMessage),
&dwNumberOfBytesWritten,
0);
assert(bStatus != 0);
}
while (strcmp("Goodbye",clientMessage) !=0);
cout << "Done" << endl;
} //end of main
Please help, will be most appreciated
Cheers