Reputation: 93
I'm having some troubles with TcpChannel
. I want to create a channel, give remote access to an object, let's say, a server and after doing all this, close the channel. The problem is that I might need to re-open the same channel later, in the same port, and I'm having some hard time trying to do this.
for connection purposes I only do:
var channel = new TcpChannel(port);
Console.WriteLine("Start Connection received at Server");
ChannelServices.RegisterChannel(channel, false);
//Initiate remote service as Marshal
RemotingServices.Marshal(this, "Server", typeof(Server));
then to close it, i just do:
Console.WriteLine("Stop Connection at Server");
channel.StopListening(null);
RemotingServices.Disconnect(this);
ChannelServices.UnregisterChannel(channel);
channel = null;
After this if I try to create a new tcpChannel instance, i get an exception saying that the tcpChannel connections are unique, they must be on different ports.
So, how can i close the tcpChannel? :S
Thanks in advance.
Upvotes: 2
Views: 4242
Reputation: 1
You will need to set the channel property: exclusiveAddressUse to false.
Upvotes: 0
Reputation: 4259
your close code are working.
recheck the logs, you miss the "Stop Connection at Server" somewhere.
Update:
there the my log (no errors):
Start Connection received at Server
Stop Connection at Server
Start Connection received at Server
Stop Connection at Server
there the implementation code:
private void button1_Click(object sender, EventArgs e)
{
channel = new TcpChannel(port);
Trace.WriteLine("Start Connection received at Server");
ChannelServices.RegisterChannel(channel, false);
//Initiate remote service as Marshal
RemotingServices.Marshal(this, "Server", typeof(Server));
}
private void button2_Click(object sender, EventArgs e)
{
Trace.WriteLine("Stop Connection at Server");
channel.StopListening(null);
RemotingServices.Disconnect(this);
ChannelServices.UnregisterChannel(channel);
channel = null;
}
Upvotes: 1
Reputation: 2743
If you just want to stop and start listening on the same port you need to explicitly call start listening. You can loose the last three lines of the code after StopListening and preserve and reuse the object until your application goes down.
channel = new TcpChannel(port);
channel.StartListening(data)
Upvotes: 0