Home : J-Integra for .NET : Simple .NET Remoting Example
Q183204 - HOWTO: Simple .NET Remoting Example

Simple .NET Remoting Example

Client accesses Server from the same machine (localhost)

Remote Objects
A remote object is implemented in a class that derives from System.MarshalByRefObject. A client doesn't call the methods directly; instead a proxy object is used to invoke methods on the remote object. Every public method that we define in the remote object class is available to be called from clients.

TestProxy.cs

using System;

namespace proxies
{
  public class TestProxy : MarshalByRefObject
  {
    private string testString;
    public TestProxy()
    {
      Console.WriteLine("TestProxy Created!.");
    }
    public void setString(string s)
    {
      testString = s + "setString in TestProxy...";
    }
    public string getString()
    {
      return testString;
    }
  }
}

Build the assembly TestProxy: csc /t:library /out:TestProxy.dll TestProxy.cs

Server
The remote object needs a server process where it will be instantiated. This server has to create a channel and put it into listening mode so that clients can connect to this channel.

Server Configuration File
In Configuration file, channel, endpoint, name etc. can be changed without a recompile. When the client connects to the remote object it needs to know the URI of the object, i.e. the name of the host where the remote object is running, the protocol and port number to connect to, the name of the server, and the name of the remote object. With the exception of the host name we have to specify all these items in the configuration file.

remoting_tcp_server.config

<configuration>
  <system.runtime.remoting>
    <application>
      <service>
        <wellknown type="proxies.TestProxy, proxies" objectUri="proxies.TestProxy.soap" mode="Singleton"/>
      </service>
      <channels>
        <channel port="3333" ref="tcp">
          <serverProviders>
            <formatter ref="binary" typeFilterLevel="Full"/>
          </serverProviders>
        </channel>
      </channels>
    </application>
  </system.runtime.remoting>
</configuration>

The Server is implemented in the console application. RemotingConfiguration.Configure() reads the configuration file to configure remoting_tcp_server.config to configure and activate the channels. We have to keep the process alive.

server.cs

using System;
using System.Runtime.Remoting;
namespace server
{
  class Server
  {
    static void Main(string[] args)
    {
      RemotingConfiguration.Configure("remoting_tcp_server.config");
      Console.WriteLine("Server is started. Press ENTER to exit");
      Console.ReadLine();
    }
  }
}

Compile from prompt: csc /t:exe server.cs

Client
Create a client using a configuration file

Client Configuration File
The client configuration file remoting_tcp_client.config uses the XML <CLIENT> element to specify the URL of the server using protocol://hostname:port/application. The application name of the server is defined with the name attribute of the <APPLICATION> element in the server configuration file. The <WELLKNOWN> element specifies the remote object we want to access. The type attribute defines the type of the remote object and the assembly. The url attribute defines the path to the remote object.

remoting_tcp_client.config

<CONFIGURATION>
  <SYSTEM.RUNTIME.REMOTING>
    <APPLICATION>
      <CLIENT>
        <WELLKNOWN type="proxies.TestProxy, proxies" url="tcp://localhost:3333/proxies.TestProxy.soap" />
      </CLIENT>
      <CHANNELS>
        <CHANNEL ref="tcp">
          <CLIENTPROVIDERS>
            <FORMATTER ref="binary" />
          </CLIENTPROVIDERS>
        </CHANNEL>
      </CHANNELS>
    </APPLICATION>
  </SYSTEM.RUNTIME.REMOTING>
</CONFIGURATION>


Client Application Activate the client channel by calling RemotingConfiguration.Configure(). Using configuration files we can simply use new to create the remote object. Next we call the method setString(string s) and getString() of this object.

client.cs

using System;
using System.Runtime.Remoting;

namespace client
{
  class Client
  {
    static void Main(string[] args)
    {
      RemotingConfiguration.Configure("remoting_tcp_client.config");
      proxies.TestProxy tp = new proxies.TestProxy();
      tp.setString("String from Client...");
      Console.WriteLine(tp.getString());
    }
  }
}

Compile from prompt: csc /t:exe /r:TestProxy.dll client.cs

Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.
Created on 7/13/2006.
Last Modified on 7/13/2006.
Last Modified by No Author Name Available!.
Article has been viewed 31619 times.
Rated 5 out of 10 based on 66 votes.
Print Article
Email Article