Nirvana C# .NET: Peer to Peer Stream-based Clients

Nirvana 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 Nirvana Realm, and is the factory object from which we can find our Service, or obtain a list of available Services:

String[] RNAME=({"nsp://127.0.0.1:9000"});
nSessionAttributes nsa = new nSessionAttributes(RNAME);
nServiceFactory factory = new nServiceFactory( nsa );

nServiceInfo info = factory.findService("example");
nEventService serv = (nEventService)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 oStream = serv.getOutputStream();
oStream.write((new UTF8Encoding()).GetBytes("Hello World"));
oStream.flush();

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 iStream = serv.getInputStream();
byte[] buff = new byte[ 100 ];
try {
  int i = 0;
  while ((i = iStream.ReadByte()) != -1)
  {
      Console.Write((char)i);
  }
} catch ( Exception ex ) {
}

Examples

The following full example source code shows how to implement a Stream-based Server Service and Client:

For more information on Nirvana Peer to Peer Services please see the API documentation.