.NET Remoting.

G

GTi

I have a program that will act as an Remoting server AND client.

This program can be loaded on one macine or it can be loaded on several
machines.
But it will only be one master program available, all the other is
"clients" handling tasks. The master program will dellegate tasks.

When the program starts it will ask if there is any "master" available
on the network (a BROADCAST to all machines). If there is no master
responds it will act as a master. Then it will look for clients. If
there is no clients available it will do all the client and server work
itself.
If a master program respond it will act as a client.

I have looked at .NET remoting and I have several questions:

When testing I put the server and client in the same program but when
registering the client I get a error:

[console printout]
The name of the channel is tcp.
The priority of the channel is 1.
The channel URI is tcp://10.47.26.120:9090.
The object URL is tcp://10.47.26.120:9090/RemoteObject.rem.
The object URI is /RemoteObject.rem.
The channel URI is tcp://10.47.26.120:9090.

Unhandled Exception: System.Runtime.Remoting.RemotingException: The
channel 'tcp' is already registered. at
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannelInternal(IChannel
chnl) at
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(IChannel
chnl)
at WindowsAgent.Program.Main(String[] args) in
C:\prog\WindowsAgent\Program.cs:line 111
Press any key to continue . . .

Does anyone have any tip of how I can solve my problem?
Using CLR 2.0
 
G

GTi

I have also testet with:
TcpServerChannel (on the server part of my program)
and
TcpClientChannel (on the client part of my program)
instead of using only TcpChannel
But with the same result.
 
N

Nicholas Paldino [.NET/C# MVP]

Without seeing your code, it's impossible to say. However, it would
seem that you have already registered the tcp channel.

Are you registering it in your config file perhaps in addition to code?

Also, as a side note, I think designing the client and the server in the
same program is a bad idea. If anything, you should segregate out the
functionality, as things like this are usually difficult to maintain.

Hope this helps.
 
G

GTi

I think using the same program as server and client makes it easier to
maintain. Perhaps the server/client program is a bad term. Let me
explain it:

This program can run on several machines, when it does it must be able
to communicate with each other (Like here I am, how are you, My name is
10.34.1.45 ). So I need to set up a tcp listen channel (server) and a
tcp sending channel (client). On the client channel I must be able to
broadcast a message to all machines on the lan. And the server channel
receives this broadcast and send a message back directly to the client
(or a broadcast depending of the message).

Doing this on two different program don't make sense to me.
I have done this in C/C++ with MailSlots. Put I don't want to p/Invoke
this API to C# since I think there is similar functions available.


[This is the simple test program]
///////////////////////////////////////////////////////////
// Create the server channel.
TcpServerChannel serverChannel = new TcpServerChannel(9090);

// Register the server channel.
ChannelServices.RegisterChannel(serverChannel);

// Show the name of the channel.
Console.WriteLine("The name of the channel is {0}.",
serverChannel.ChannelName);

// Show the priority of the channel.
Console.WriteLine("The priority of the channel is {0}.",
serverChannel.ChannelPriority);

// Show the URIs associated with the channel.
ChannelDataStore data = (ChannelDataStore)serverChannel.ChannelData;
foreach (string uri in data.ChannelUris)
{
Console.WriteLine("The channel URI is {0}.", uri);
}

// Expose an object for remote calls.
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject),
"RemoteObject.rem", WellKnownObjectMode.Singleton);

// Parse the channel's URI.
string[] urls = serverChannel.GetUrlsForUri("RemoteObject.rem");
if (urls.Length > 0)
{
string objectUrl = urls[0];
string objectUri;
string channelUri = serverChannel.Parse(objectUrl, out objectUri);
Console.WriteLine("The object URL is {0}.", objectUrl);
Console.WriteLine("The object URI is {0}.", objectUri);
Console.WriteLine("The channel URI is {0}.", channelUri);
}

// Wait for the user prompt.
// Console.WriteLine("Press ENTER to exit the server.");
// Console.ReadLine();




///////////////////////////////////////////////////
// Create the channel.
TcpClientChannel clientChannel = new TcpClientChannel();


// Register the channel.
ChannelServices.RegisterChannel(clientChannel);

// Register as client for remote object.
WellKnownClientTypeEntry remoteType = new
WellKnownClientTypeEntry(typeof(RemoteObject),
"tcp://localhost:9090/RemoteObject.rem");
RemotingConfiguration.RegisterWellKnownClientType(remoteType);

// Create a message sink.
string cobjectUri;
System.Runtime.Remoting.Messaging.IMessageSink messageSink =
clientChannel.CreateMessageSink("tcp://localhost:9090/RemoteObject.rem",
null, out cobjectUri);
Console.WriteLine("The URI of the message sink is {0}.", cobjectUri);
if (messageSink != null)
{
Console.WriteLine("The type of the message sink is {0}.",
messageSink.GetType().ToString());
}

// Create an instance of the remote object.
RemoteObject service = new RemoteObject();

// Invoke a method on the remote object.
Console.WriteLine("The client is invoking the remote object.");
for (int a = 0; a < 100; a++)
{
Console.WriteLine("The remote object has been called {0} times.",
service.GetCount());
}










Without seeing your code, it's impossible to say. However, it would
seem that you have already registered the tcp channel.

Are you registering it in your config file perhaps in addition to code?

Also, as a side note, I think designing the client and the server in the
same program is a bad idea. If anything, you should segregate out the
functionality, as things like this are usually difficult to maintain.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

GTi said:
I have a program that will act as an Remoting server AND client.

This program can be loaded on one macine or it can be loaded on several
machines.
But it will only be one master program available, all the other is
"clients" handling tasks. The master program will dellegate tasks.

When the program starts it will ask if there is any "master" available
on the network (a BROADCAST to all machines). If there is no master
responds it will act as a master. Then it will look for clients. If
there is no clients available it will do all the client and server work
itself.
If a master program respond it will act as a client.

I have looked at .NET remoting and I have several questions:

When testing I put the server and client in the same program but when
registering the client I get a error:

[console printout]
The name of the channel is tcp.
The priority of the channel is 1.
The channel URI is tcp://10.47.26.120:9090.
The object URL is tcp://10.47.26.120:9090/RemoteObject.rem.
The object URI is /RemoteObject.rem.
The channel URI is tcp://10.47.26.120:9090.

Unhandled Exception: System.Runtime.Remoting.RemotingException: The
channel 'tcp' is already registered. at
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannelInternal(IChannel
chnl) at
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(IChannel
chnl)
at WindowsAgent.Program.Main(String[] args) in
C:\prog\WindowsAgent\Program.cs:line 111
Press any key to continue . . .

Does anyone have any tip of how I can solve my problem?
Using CLR 2.0
 
G

GTi

// Remote object.
public class RemoteObject : MarshalByRefObject
{
private int callCount = 0;

public int GetCount()
{
callCount++;
return (callCount);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top