Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#
Article

.NET Remoting Sample

Rate me:
Please Sign up or sign in to vote.
4.52/5 (50 votes)
14 Apr 20052 min read 245.8K   31.4K   138   26
Shows how to use .NET Remoting for beginners.

Introduction

Why am I publishing this sample?

Because it is very difficult to find working code in the MSDN or internet. Most samples require awkward config files or they are much too complicated for beginners. This sample runs immediately and does not require config files.

The original intention was to remote control an application on a slave computer via network. The master sends a command and waits for the slave until it executes the remote command. This sample sends only text strings, but it can easily be expanded to transfer anything. (See class cTransfer.)

Features

  • Demonstration of .NET Remoting.
  • Very simple and easy to understand code!!
  • No awkward config files necessary (Port and Host are specified in the GUI).
  • Master.exe (client) and Slave.exe (host) can either run on the same computer (localhost) or on different computers in the network.
  • Multiple Masters can connect to one Slave on the same port (but not at once)
  • Multiple instances of Masters and Slaves can communicate independently from each other on different ports.
  • To keep this sample simple, it uses a blocking call to the Slave. This means the Master waits for the response of the Slave. (This may be useful for sending commands to another computer and get the result after the Slave is ready with executing the remote command.)

Image 1

Image 2

How does it work?

Image 3

Programming .NET Remoting

There a three major ways to use .NET Remoting (this sample uses the first one):

Publishing a public object

(Object is created locally and then published.)

Host:

C#
ChannelServices.RegisterChannel (new TcpChannel(1500));
cTransfer Trans = new cTransfer();
RemotingServices.Marshal (Trans, "TestService");

Client:

C#
cTransfer T = (cTransfer) Activator.GetObject(typeof(cTransfer), 
                                 "tcp://host:1500/TestService"); 

Remote creation of a public object (SAO)

(Object is created on request of client)

Host:

C#
ChannelServices.RegisterChannel (new TcpChannel(1500));
RemotingConfiguration.RegisterWellKnownServiceType(typeof(cTransfer), 
    "TestService", WellKnownObjectMode.Singleton);

Client:

C#
cTransfer T = (cTransfer) Activator.GetObject(typeof(cTransfer), 
                                 "tcp://host:1500/TestService");

Remote creation of a private object (CAO)

(Object is created on host and client receives a reference to it)

Host:

C#
ChannelServices.RegisterChannel (new TcpChannel(1500));
RemotingConfiguration.RegisterActivatedServiceType(typeof(cTransfer));

Client:

C#
object[] attr = {new UrlAttribute("tcp://host:1500")};
object[] args = {"Sample constructor argument"};
cTransfer T = (cTransfer) Activator.CreateInstance(typeof(cTransfer), args, attr);

Notes:

  • cTransfer is a class which is defined in Remoting.DLL. It contains the variables and an event to communicate between Host and Client.
  • These samples use port 1500. You can use any TCP port which is unused and not blocked by the firewall.
  • The Endpoint used here is "TcpService". You can use any name, but Host and Client must always use the same endpoint name!

This should be enough of explanation. Look into the source code: it's really simple!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
mrezax2-Aug-11 5:33
mrezax2-Aug-11 5:33 
QuestionGoing to try and use this in a board game - Suggestions / Advice, please? Pin
zack_falcon18-Jul-11 8:45
zack_falcon18-Jul-11 8:45 
GeneralMy vote of 3 Pin
paulsasik11-Oct-10 11:49
professionalpaulsasik11-Oct-10 11:49 
GeneralThanks alot.. Pin
Srikanth. Vemulapalli7-Feb-10 3:32
Srikanth. Vemulapalli7-Feb-10 3:32 
QuestionHaving problems Converting it to VB.net Pin
imavanloon19-Dec-09 1:47
imavanloon19-Dec-09 1:47 
GeneralGood Article but how to compile and run. Pin
rajath888811-Apr-09 1:24
rajath888811-Apr-09 1:24 
GeneralThanks a lot Pin
Bala00120-Dec-08 23:06
Bala00120-Dec-08 23:06 
GeneralRe: Thanks a lot Pin
NathanY30-Mar-09 0:54
NathanY30-Mar-09 0:54 
GeneralRe: Thanks a lot Pin
Andrew Habegger9-Nov-09 8:53
Andrew Habegger9-Nov-09 8:53 
QuestionHow to make multiple slave available? Pin
Alan8830-Jul-07 3:45
Alan8830-Jul-07 3:45 
General.Net Remoting Pin
D Nagendra Prasad28-Feb-07 19:17
D Nagendra Prasad28-Feb-07 19:17 
General"Cross-thread operation not valid" Pin
Mark197531-Oct-06 17:51
Mark197531-Oct-06 17:51 
AnswerRe: "Cross-thread operation not valid" Pin
Greg Mulvihill2-Nov-06 13:43
Greg Mulvihill2-Nov-06 13:43 
QuestionMaster cant receive the message from the client.. why? Pin
kyithar15-Oct-06 11:55
kyithar15-Oct-06 11:55 
QuestionRemoting CAO in .NET 2005 Pin
Suranjan Nandi19-Jun-06 20:31
Suranjan Nandi19-Jun-06 20:31 
GeneralRemoting.Dll Pin
attias gabi27-Apr-06 5:40
attias gabi27-Apr-06 5:40 
GeneralRe: Remoting.Dll Pin
Elmue2-May-06 4:00
Elmue2-May-06 4:00 
GeneralThanks Pin
SavitruNarayan22-Feb-06 1:52
SavitruNarayan22-Feb-06 1:52 
GeneralGood Article Pin
Maitreya Kapasi22-Apr-05 2:16
Maitreya Kapasi22-Apr-05 2:16 
Questionhow can i catter more than one Slave? Pin
supudu20-Apr-05 19:00
supudu20-Apr-05 19:00 
AnswerRe: how can i catter more than one Slave? Pin
Anonymous21-Apr-05 2:03
Anonymous21-Apr-05 2:03 
GeneralError while i chage this code to vb.net Pin
Gpawan17-Apr-05 5:26
Gpawan17-Apr-05 5:26 
GeneralRe: Error while i chage this code to vb.net Pin
chushumor15-Nov-07 2:22
chushumor15-Nov-07 2:22 
GeneralAdditional reads Pin
mav.northwind6-Apr-05 23:10
mav.northwind6-Apr-05 23:10 
GeneralRe: Additional reads Pin
Anonymous7-Apr-05 1:08
Anonymous7-Apr-05 1:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.