You're getting the error because a new server instance is created that starts listening on port 5000 (Server s = new Server();
), then when a new remoted instance is created its default constructor is run and tries to do the same.
Do you need a server to listen separately on port 5000? If you do then you could create another constructor that is run for the non-remoted instance:
class Server : MarshalByRefObject, ServerInterface
{
public Server()
{
}
public Server(bool local)
{
Listener();
}
private void Listener()
{
TcpListener listener = new TcpListener(IPAddress.Any, 5000);
listener.Start();
while (offline == false)
{
TcpClient client = listener.AcceptTcpClient();
}
}
}
...
TcpChannel channel = new TcpChannel(5002);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Server),
"Server", WellKnownObjectMode.SingleCall);
Server s = new Server(true);