Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Visual Basic help required


  • Please log in to reply

#1
SuperGandhi

SuperGandhi

    Member

  • Member
  • PipPip
  • 26 posts
I'm currently completing(!?) a course through my work which is on C++ and Visual Basic. I did an elective in VB at uni when i was there, but if i'm honest, can barely remember where i live, let alone, what i 'learned' (;) )at uni!!!

I want to create a chat application that exchanges messages through a named pipe. The idea being that I have a named pipe server that kicks off and waits for the client to respond. when the client responds a message box shows up and displays the coming/going messages, then closes when given a specific prompt.

Here's what i've got for the server:

#include <windows.h>
#include <iostream>
#include <assert.h>

using namespace std;

void main()
{
cout << "Server process running" << endl;

HANDLE hNamedPipe = CreateNamedPipe( "\\\\.\\pipe\\Newpipe",
PIPE_ACCESS_INBOUND,
0,
1,
0,
0,
0,
0);
assert(hNamedPipe != INVALID_HANDLE_VALUE);

BOOL bStatus = ConnectNamedPipe(hNamedPipe, 0);
assert(bStatus != 0);
cout << "Client has connected" << endl;


DWORD dwNumberOfBytesRead;
char clientMessage[80];
do
{

bStatus = ReadFile( hNamedPipe,
clientMessage,
79,
&dwNumberOfBytesRead,
0);
assert(bStatus!=0);


clientMessage[dwNumberOfBytesRead] = '\0';


cout << "Client Message is: " <<
clientMessage <<
endl;

}
while (strcmp("closepipe",clientMessage) != 0);


bStatus = DisconnectNamedPipe(hNamedPipe);
assert(bStatus != 0);
CloseHandle(hNamedPipe);

cout << "Done" << endl;

}


This was half taken from the notes we were sent out, but frankly I don't really have a clue :tazz: , anybody help???

Edited by SuperGandhi, 13 August 2005 - 12:13 PM.

  • 0

Advertisements


#2
SuperGandhi

SuperGandhi

    Member

  • Topic Starter
  • Member
  • PipPip
  • 26 posts
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 :tazz:
  • 0

#3
stu_design

stu_design

    Member

  • Member
  • PipPipPip
  • 217 posts
are u using the same pipe to communicate?

just being "devil's advocate"
  • 0






Similar Topics

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP