Thursday, June 19, 2008

One Way .NET 2.0 Remoting (with source code).

  • This post contains generic code that's ready for use.
  • The code was tested and debugged.
  • Full solution is available at the end of the post.

Preface

In this post you can find all the code that you need in order to establish remoting communication in one direction (from client to server).

While establishing one way inter process communication via remoting is fairly simple, things get pretty messy when clients also need to listen to server events.  Please refer to ".NET Remoting with Events" for detailed review and case study. 

One Way Client-Server Communication via Remoting.

In this article we will review the most simple implementation of a remoting based client-server application, in which communication flows in only one way; that is, the client is able to initiate method execution on server objects, while the server object is stateless and has no way to initiate remote calls on the client.

Server-Side
Remotable object

RemotingDistributableObject the server object that clients can remotely activate. Clients can get proxy for this object and call methods that exposed through IRemotingDistributableObject interface.

public class RemotingDistributableObject : MarshalByRefObject, IRemotingDistributableObject
{
    public void SendMessage(string p_mes)
    {
        ServerImp.Instance.RespondToMessage(p_mes);
    }
}
Server Terminal

This terminal is used to instantiated the server remotable object.

public class OneWayServerTerminal 
{
    public static void StartListening(int port)
    {
        TcpServerChannel channel = new TcpServerChannel(
            Constants.RemotingChannelName, port);

        ChannelServices.RegisterChannel(channel, false);

        WellKnownServiceTypeEntry remObj = new WellKnownServiceTypeEntry
        (
            typeof(RemotingDistributableObject),
            "RemotingDistributableObject",
            WellKnownObjectMode.SingleCall
        );

        RemotingConfiguration.RegisterWellKnownServiceType(remObj);
    }
}
Server Host (Console)

All server host has to do is to call StartListening through the server terminal. After that call - clients can retrieve proxy for the server object.

OneWayServerTerminal.StartListening(1234);

 

Both-Sides
Remotable object interface

IRemotingDistributableObject is the interface that the remotable object implements, the proxy that the client retrieves implement this interface.

public interface IRemotingDistributableObject
{
    void SendMessage(string p_mes);
}
Client Terminal

OneWayClientTerminal is used to retrieve proxy for the server object.

public class OneWayClientTerminal
{
    private TcpClientChannel m_Channel;

    public IRemotingDistributableObject Connect(string serverName, int port)
    {
        m_Channel = new TcpClientChannel(Constants.RemotingChannelName, null);

        try
        {
            ChannelServices.RegisterChannel(m_Channel, false);
        }
        catch
        {
            m_Channel = null;
            throw;
        }

        string fullServerAddress = string.Format(
            "tcp://{0}:{1}/RemotingDistributableObject", serverName, port);

        IRemotingDistributableObject videoServerTerminal = 
            (IRemotingDistributableObject)Activator.GetObject(
            typeof(IRemotingDistributableObject), fullServerAddress);

        return videoServerTerminal;            
    }

    public void Disconnect()
    {
        if (m_Channel != null)
        {
            ChannelServices.UnregisterChannel(m_Channel);
        }
    }
}
Client-Side
Client Host (Console)

Client host creates instance of OneWayClientTerminal, connects to the server and retrieve proxy for the server object. Later on, it can use the ClientTerminal object in order to disconnect from the server.

m_Terminal = new OneWayClientTerminal();
m_RemoteProxy = m_Terminal.Connect("localhost", 1234);

Sample project

Download from here

Interesting links

http://www.codeproject.com/KB/IP/cs_remoting.aspx

1 comment:

  1. Is the sample source download available anywhere else? I've checked back a couple time and I keep getting a "too many connections" response.

    ReplyDelete