Click here to Skip to main content
Email Password   helpLost your password?

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

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:

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralThanks alot..
Srikanth. Vemulapalli
4:32 7 Feb '10  
Thanks alot, this is what I am looking for .. Thanks again.
Srikanth.Vemulapally

QuestionHaving problems Converting it to VB.net
Member 4087942
2:47 19 Dec '09  
Is there anyone who is able to convert this excellent sample to VB?

Getting an error:

Events cannot be declared with a delegate type that has a return type

Even after a workaround, having problems to use the "function"

Remote.kResponse k_Response = mi_Transfer.CallSlave(k_Action)

in vb.

Thanks in advance,

alex (imavanloon@hotmail.com)
GeneralGood Article but how to compile and run.
rajath8888
2:24 11 Apr '09  
I am a beginner to .net remoting. I understood this wonderful article. Can any1 please explain the steps on how to run this code through visual studio?
GeneralThanks a lot
Bala001
0:06 21 Dec '08  
Hi Helmut:

Thanks for the wonderful and concise working sample. It works like a charm.
GeneralRe: Thanks a lot
NathanY
1:54 30 Mar '09  
Here Here...
GeneralRe: Thanks a lot
Andrew Habegger
9:53 9 Nov '09  
Very helpful. Thank you.
GeneralHow to make multiple slave available?
Alan88
4:45 30 Jul '07  
I think this code just can support 1 master vs 1 slave & multiple master vs slave. Can i have 1 master vs multiple slave? How to do it?
General.Net Remoting
D Nagendra Prasad
20:17 28 Feb '07  


"Error sending message to Slave: Cross-thread operation not valid: Control 'textBoxMessage' accessed from a thread other than the thread it was created on."


varsha , subham_msc@yahoo.com
General"Cross-thread operation not valid"
Mark1975
18:51 31 Oct '06  
When running the app from C# 2005, I click 'send' on the Master, and it gives the error:

"Error sending message to Slave: Cross-thread operation not valid: Control 'textBoxMessage' accessed from a thread other than the thread it was created on."

Sorry, I currently lack the skills to track down the cause of the problem.

AnswerRe: "Cross-thread operation not valid"
gregmulvihill
14:43 2 Nov '06  
In OnMasterEvent (Slave.frmMain), wrap textBoxMessage.Text like this...


this.Invoke((System.Threading.ThreadStart)delegate()
{
textBoxMessage.Text = string.Format("Message from [{0}]:\r\n{1}\r\n(Click Button \"Send\" to answer)", k_Action.s_Computer, k_Action.s_Command);
});

GeneralMaster cant receive the message from the client.. why?
kts2005s
12:55 15 Oct '06  
Sniff
QuestionRemoting CAO in .NET 2005
Suranjan Nandi
21:31 19 Jun '06  
Hi,
I am using COA for remoting coding.
My codes are working very fine in .NET 2003, but is going to hang position in .NET 2005. Just after the proxy is created on the client side it is going to hang position.
Please suggest.

GeneralRemoting.Dll
attias gabi
6:40 27 Apr '06  
Can u post the code for the dll!? thanks
GeneralRe: Remoting.Dll
Elmue
5:00 2 May '06  


Häähhh ????

Click on download !
GeneralThanks
Narayan Ambatipudi
2:52 22 Feb '06  
Hi Thanks alot.

I have a doubt.If I create both Server and Client in same windows application and try to open channel for two time then I am getting Channel already opened Exception.But If I split the same logic into different application then its working fine.I know that there is a conflict in same thread.But can u justify this more clearly.

I ll be happy if u can post u r reply to technicalmail@gmail.com.

regards,
Narayan.

VS.Narayan.Ambatipudi
GeneralGood Article
Maitreya Kapasi
3:16 22 Apr '05  
Hi .... Its really a very good article ..

Keep it up Smile
Generalhow can i catter more than one Slave?
supudu
20:00 20 Apr '05  
hai there
I should say u have provided a pretty good article to begin with but in this one u are stating at this one is only going to work with single Master and Single Slave.....
but how to handle when there are more than one single Slave????????

Thanks in Adavance

AustinSmile
GeneralRe: how can i catter more than one Slave?
Anonymous
3:03 21 Apr '05  
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
Gpawan
6:26 17 Apr '05  
Public Delegate Function del_SlaveCall(ByVal k_Action As kAction) As kResponse
Public Event ev_SlaveCall As del_SlaveCall

But the ERROR says, EVENT CAN'T BE DECLARED WITH A DELEGATE TYPE THAT HAS A RETURN TYPE. CAN YOU LET ME KNOW WHY?
GeneralRe: Error while i chage this code to vb.net
chushumor
3:22 15 Nov '07  
Hi, I have the same problem
did you resolve this error?
Thanks
Nachi
GeneralAdditional reads
mav.northwind
0:10 7 Apr '05  
For all of you who are starting to use .NET Remoting, there's lots of extremely valuable information on the topic to be found on thinktecture[^], written by Ingo Rammer.

Practically a must after your first steps on this terrain, especially if you start wondering why some things do not work as expected... :->

Regards,
mav
GeneralRe: Additional reads
Anonymous
2:08 7 Apr '05  
Hello

Interesting article.
But 100% theory.
0% code samples
So not 100% useful!

Ramone


GeneralRe: Additional reads
mav.northwind
2:13 7 Apr '05  
True, it's not really newbie-compatible Wink
But as soon as one starts dwelling deeper into remoting the infos there are really essential...

mav


Last Updated 15 Apr 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010