Chat Server and Client in C# Using Remoting Technology

Introduction

The application is a simple Chat Server and Client, which is the conversion of a Java RMI chat server and client. The Java client was an Applet and present application client in WinForm.

You can find the original Java code from [Troy Downing [email protected] http://www.webcal.com/downing/ February 1997].

Technology Used

  1. Serialization: Serialization is the process of converting a graph of objects, into a linear sequence of bytes. That sequence of bytes can be sent elsewhere (for example, to a remote computer) and Deserialized, thereby making a clone in that remote memory of the original graph of objects. Serialization is necessary for this application to have the state of the Remote Object on the client and server at the same time.
  2. Remoting: Microsoft .NET Remoting provides a rich and extensible framework for objects living in different AppDomains, in different processes, and in different machines to communicate with each other seamlessly. .NET Remoting offers a very powerful yet simple programming model and runtime support for making these interactions transparent.

.NET Remoting Objects

There are three types of objects that can be configured to serve as .NET remote objects. You can choose the type of object depending on the requirements of your application. This section explains these objects in detail.

Single Call

"Single Call" objects service one and only one request coming in. Single call objects are useful in scenarios where the objects are required to do a finite amount of work and are usually not required to store state information. Single-call objects can be configured in a load-balanced fashion. Single Call Objects cannot hold state information between method calls.

Singleton

"Singleton" Objects are those objects that service multiple clients and hence share data by storing state between client invocations. They are useful in cases where data needs to be explicitly shared between clients and also where the overhead of creating and maintaining objects is substantial.

In the sample, we have to use singleton because of the nature of the application.

Client Activated Objects

Client Activated Objects (CAO) are server-side objects that are activated on request from the client. This way of activating server objects is very similar to the classic COM coclass activation. When the client requests a server object using the "new" operator, an activation request message is sent to the remote application. The server then creates an instance of the requested class and returns an ObjRef back to the client application that invoked it. A proxy is then created on the client side using the ObjRef. The client's method calls will be executed on the proxy. Client Activated Objects can store state information between method calls for its specific client and not across different client objects. Each invocation of "new" returns a proxy to an independent instance of the server type.

Passing Objects using .NET remoting

In .NET Remoting, objects can be passed from one application to another in the following ways.

  1. As parameters in the method call
    • Example
      public int MyRemoteMethod(MyRemoteObject myObj)
      
  2. Return Value of method calls
    • Example
      public MyRemoteObject myRemoteMethod(String myString)
      
  3. Values resulting from property or field access of a .NET component
    • Example
      myObj.myNestedObject
      

For objects that are Marshal By Value (MBV), a complete copy of the object is made when the object is passed from one application to another.

For objects that are Marshal By Reference (MBR), a reference to the object is made when passed from one application to another. When the object reference (ObjRef) arrives in the remote application, it is turned into a "proxy" back to the original object.

Hosting .NET remoting objects

.NET Remoting objects can be hosted in.

  1. Managed Executable: .NET Remoting objects can be hosted in any regular .NET EXE or a managed Service.
  2. IIS: Remoting objects can be hosted in the Internet Information Server (IIS).
  3. .NET Component Services: .NET Remoting objects can be hosted in the .NET component services Infrastructure to take advantage of the various COM+ services like Transactions, JIT, Object pooling, etc.

Channel Services (System.Runtime.Remoting.Channels)

The .NET framework supplies the HTTP, TCP, and SMTP channels but third parties can write and plug in their own channels. The HTTP and SMTP channels use SOAP by default to communicate whereas the TCP channel uses Binary payload by default.

The Present Application uses a TCP channel to communicate.

About the sample

The server is a console application and the client is Win Form. This application uses the Remoting Namespace of .NET to transfer the Server Object on the wire and gets created on the Client. Remoting can be done in .NET by many means (channels), the TCP channel is used in the present sample. You can use an HTTP channel or SOAP to do this too.

Good luck and Enjoy C#.


Recommended Free Ebook
Similar Articles