Click here to Skip to main content
Click here to Skip to main content

.NET Remoting Sample

, 14 Apr 2005
Rate this:
Please Sign up or sign in to vote.
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.)

How does it work?

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:

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

Client:

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:

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

Client:

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:

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

Client:

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

Share

About the Author

Helmut Güldenagel

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pinmembermrezax2-Aug-11 6:33 
QuestionGoing to try and use this in a board game - Suggestions / Advice, please? Pinmemberzack_falcon18-Jul-11 9:45 
GeneralMy vote of 3 Pinmemberpaulsasik11-Oct-10 12:49 
GeneralThanks alot.. PinmemberSrikanth. Vemulapalli7-Feb-10 4:32 
QuestionHaving problems Converting it to VB.net PinmemberMember 408794219-Dec-09 2:47 
GeneralGood Article but how to compile and run. Pinmemberrajath888811-Apr-09 2:24 
GeneralThanks a lot PinmemberBala00121-Dec-08 0:06 
GeneralRe: Thanks a lot PinmemberNathanY30-Mar-09 1:54 
GeneralRe: Thanks a lot PinmemberAndrew Habegger9-Nov-09 9:53 
QuestionHow to make multiple slave available? PinmemberAlan8830-Jul-07 4:45 
General.Net Remoting PinmemberD Nagendra Prasad28-Feb-07 20:17 
General"Cross-thread operation not valid" PinmemberMark197531-Oct-06 18:51 
AnswerRe: "Cross-thread operation not valid" Pinmembergregmulvihill2-Nov-06 14:43 
QuestionMaster cant receive the message from the client.. why? Pinmemberkts2005s15-Oct-06 12:55 
QuestionRemoting CAO in .NET 2005 PinmemberSuranjan Nandi19-Jun-06 21:31 
GeneralRemoting.Dll Pinmemberattias gabi27-Apr-06 6:40 
GeneralRe: Remoting.Dll PinmemberElmue2-May-06 5:00 
GeneralThanks PinmemberNarayan Ambatipudi22-Feb-06 2:52 
GeneralGood Article PinmemberMaitreya Kapasi22-Apr-05 3:16 
Questionhow can i catter more than one Slave? Pinmembersupudu20-Apr-05 20:00 
AnswerRe: how can i catter more than one Slave? PinsussAnonymous21-Apr-05 3:03 
Hello
 
It depends on what you want to do.
 
Because this simple sample demonstrates something like "chatting" the slave must block further master calls until the previous call has been answered by a user response.
 
You can remove this code in the slave:
 
if (mb_WaitButton)
{
k_Response.s_Result = "Sorry! Slave is currently busy.\r\nTry again later";
return k_Response;
}

 
and also remove:
 
while (mb_WaitButton)
{
Thread.Sleep(200);
};

 
Then you can send text messages from any number of masters and they will be immediately displayed in the slave's edit box.
 
BUT:
The response will not work properly anymore.
The response of the slave will always be the same. The user will have no chance to enter a text, because the function returns immediately.
 
So the question is what you want to do ?
 
If you don't need a response from the slave which requires user interaction, you are ready now. (For example if the slave does a short calculation and sends the resoponse immediately)
 
But if you need a "real" asynchronous bidirectional communication then put the same listening mechanism also into the master.
You can even let the master and slave listen on the same port if they do not run on the same machine.
Then you don't have a slave and a master anymore. Then both are equal peers.
 
Then you can do a real chat program.
But I did'nt want to make sample unnecessary complicated.
 
Helmut

GeneralError while i chage this code to vb.net PinmemberGpawan17-Apr-05 6:26 
GeneralRe: Error while i chage this code to vb.net Pinmemberchushumor15-Nov-07 3:22 
GeneralAdditional reads Pinmembermav.northwind7-Apr-05 0:10 
GeneralRe: Additional reads PinsussAnonymous7-Apr-05 2:08 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 15 Apr 2005
Article Copyright 2005 by Helmut Güldenagel
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid