Peer to Peer Stream-based Clients
Universal Messaging Peer to Peer Stream-based Services communicate via input and output streams on both the Stream-based Client and the Stream-based Server Service.
Anything written to the output stream of the Stream-based Service Client is received via the input stream of the Stream-based Server Service and vice versa.
Creating a Stream-based Client
The nServiceFactory object establishes a connection with the Universal Messaging Realm, and is the factory object from which we can find our Service, or obtain a list of available Services:
nSessionAttributes *nsa = new nSessionAttributes(RNAME);
nServiceFactory *factory = new nServiceFactory( nsa );
nServiceInfo *info = factory->findService("example");
nStreamService *serv = (nStreamService *)factory->connectToService( info );
Once the Client has connected to an instance of a Server Service, the developer's custom business logic can then be applied.
Writing Client Data to a Stream-based Server Service
Once a client has connected to a Service, the client can write data to the Service. The client can obtain a reference to the Service's Output Stream object and then write to it as follows:
Stream *os = serv->getOutputStream();
//Read a character from the standard input until a \n is reached which indicates
// the end of a command.
int pos = 0;
bool run = true;
command[pos] = getchar();
if (command[pos] == '\n')
if (pos > 0)
unsigned char *temp = new unsigned char[pos];
memcpy (temp, command, pos);
os->write(temp, 0, pos);
pos = 0;
run = false;
Receiving Responses from a Stream-based Server Service
To receive responses from the Service, the client must first obtain a reference to the Service's Input Stream object, and then read from it as follows:
Stream *is = serv->getInputStream();
unsigned char *pBuf = new unsigned char[bufferSize];
int size = 0;
int i = 0;
while ((i = is->readByte()) != -1)