Remoting Chat Application

Concept

The application is designed using the .NET remoting framework 1.0. .NET remoting provides a very powerful framework for distributed applications. When you design a chat application, you need to maintain the state of the client at the server side. That means the server and clients must be in continuous contact. By using CAO (client activated objects), it is not possible to maintain state at the server side. Server-activated objects with singleton (a single server object serving all clients) design is the best methodology to develop a chat application. The application uses server-activated singleton object. A shared interface approach is used for communication between the server and client. This prevents the client from directly creating a server object.

Implementation

There are three projects in the application: ChatClient, ChatServer, and ChatMediator. ChatServer is a normal remoting server that runs on TCP (5678) and HTTP (8888) channels. The ChatMediator project is a common interface shared across client and server. ChatClient is the client that can connect to the server either by a TCP or HTTP channel. Following are the details of each project and how they work.

ChatServer

ChatServer is the simple remoting server. It also contains an internal class that derives from MarshalByRef and implements the common interface shared across client and server.

Here is the code of Server class.

/***************************************************************/
class Server
   {
      private HttpChannel htc;
      private TcpChannel tcc;
      private int httpPortNumber = 8888;
      private int tcpPortNumber = 5678;

      public Server()
      {
         // Register Http channel
         SoapClientFormatterSinkProvider scl = null;
         SoapServerFormatterSinkProvider ssr =
            new SoapServerFormatterSinkProvider();
         ssr.TypeFilterLevel = TypeFilterLevel.Full;
         IDictionary id = new Hashtable();
         id["port"] = httpPortNumber;
         id["typeFilterLevel"] = TypeFilterLevel.Full;
         id["name"] = System.Guid.NewGuid().ToString();
         htc = new HttpChannel(id, scl, ssr);
         ChannelServices.RegisterChannel(htc);
         id.Clear();
         id = null;

         // Register Tcp channel
         BinaryClientFormatterSinkProvider bcfs = null;
         BinaryServerFormatterSinkProvider bsfs =
            new BinaryServerFormatterSinkProvider();
         bsfs.TypeFilterLevel = TypeFilterLevel.Full;
         id = new Hashtable();
         id["port"] = tcpPortNumber;
         id["typeFilterLevel"] = TypeFilterLevel.Full;
         id["name"] = System.Guid.NewGuid().ToString();
         tcc = new TcpChannel(id, bcfs, bsfs);
         ChannelServices.RegisterChannel(tcc);
         id.Clear();
         id = null;
      }

      public static void StartServer()
      {
         try
         {

RemotingConfiguration.RegisterWellKnownServiceType(typeof(Mediator),
                      "ChatServer.soap",
            WellKnownObjectMode.Singleton);
         }
         catch (RemotingException ex)
         {
            Console.WriteLine(ex.Message);
            throw new UserExceptions(ex.Message, ex);
         }
      }

   /// <summary>
   /// The main entry point for the application.
   /// </summary>
   [STAThread]
   static void Main(string[] args)
   {
      //
      // TODO: Add code to start application here
      //
      if (args != null && args.Length > 0)
      {
         Console.WriteLine(args[0]);
      }

try
      {

Console.WriteLine(
   "/********************************************************/");
Console.WriteLine("**tWelcome to chat application developed by-:");
Console.WriteLine("**tJayant D. Kulkarni,
                      Mahindra-British Telecom.");
        Console.WriteLine("**tThis is server.");
        Server srs = new Server();
        Server.StartServer();
        Console.WriteLine("**tServer started successfuly.");
        Console.WriteLine("**tListening at.");
        Console.WriteLine("**tHttpChannel: 8888 and TcpChannel:
                              5678.");

Console.WriteLine(
   "/********************************************************/nn");
      }
      catch (Exception ex)
      {
Console.WriteLine("Error occured. Message: " + ex.Message);
      }

      Console.ReadLine();
   }
}
Userdefined exception class
   /**************************************************************/
internal class UserExceptions : ApplicationException
   {
      internal UserExceptions(string message) : base(message)
      {
      }

internal UserExceptions(string message, RemotingException ex) :
         base(message, ex)
      {
      }
   }
   /**************************************************************/

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read