Click here to Skip to main content
6,597,576 members and growing! (19,843 online)
Email Password   helpLost your password?
General Programming » Internet / Network » Remoting     Intermediate

.NET Remoting Sample

By Helmut Güldenagel

Shows how to use .NET Remoting for beginners.
C#, Windows, .NET 1.1VS.NET2003, Dev
Posted:6 Apr 2005
Updated:14 Apr 2005
Views:99,648
Bookmarked:101 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
41 votes for this article.
Popularity: 6.80 Rating: 4.21 out of 5
3 votes, 7.3%
1
2 votes, 4.9%
2
3 votes, 7.3%
3
12 votes, 29.3%
4
21 votes, 51.2%
5

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

About the Author

Helmut Güldenagel


Member

Location: United States United States

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 20 of 20 (Total in Forum: 20) (Refresh)FirstPrevNext
GeneralGood Article but how to compile and run. Pinmemberrajath88882:24 11 Apr '09  
GeneralThanks a lot PinmemberBala0010:06 21 Dec '08  
GeneralRe: Thanks a lot PinmemberNathanY1:54 30 Mar '09  
GeneralHow to make multiple slave available? PinmemberAlan884:45 30 Jul '07  
General.Net Remoting PinmemberD Nagendra Prasad20:17 28 Feb '07  
General"Cross-thread operation not valid" PinmemberMark197518:51 31 Oct '06  
AnswerRe: "Cross-thread operation not valid" Pinmembergregmulvihill14:43 2 Nov '06  
GeneralMaster cant receive the message from the client.. why? Pinmemberkts2005s12:55 15 Oct '06  
QuestionRemoting CAO in .NET 2005 PinmemberSuranjan Nandi21:31 19 Jun '06  
GeneralRemoting.Dll Pinmemberattias gabi6:40 27 Apr '06  
GeneralRe: Remoting.Dll PinmemberElmue5:00 2 May '06  
GeneralThanks PinmemberNarayan Ambatipudi2:52 22 Feb '06  
GeneralGood Article PinmemberMaitreya Kapasi3:16 22 Apr '05  
Generalhow can i catter more than one Slave? Pinmembersupudu20:00 20 Apr '05  
GeneralRe: how can i catter more than one Slave? PinsussAnonymous3:03 21 Apr '05  
GeneralError while i chage this code to vb.net PinmemberGpawan6:26 17 Apr '05  
GeneralRe: Error while i chage this code to vb.net Pinmemberchushumor3:22 15 Nov '07  
GeneralAdditional reads Pinmembermav.northwind0:10 7 Apr '05  
GeneralRe: Additional reads PinsussAnonymous2:08 7 Apr '05  
GeneralRe: Additional reads Pinmembermav.northwind2:13 7 Apr '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Apr 2005
Editor: Sean Ewington
Copyright 2005 by Helmut Güldenagel
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project