Frage

I'm basically making a Client/Server application and another software which gets information from the server (monitor). I'm doing this and have to using .NET remoting.

I get the error when I try to establish a connection between my monintor and server. My server listens for connections on port 5000, where my remoting listens on port 5002.

This is how I listen in my server:

 class Server : MarshalByRefObject, ServerInterface
 {
    public Server()
    {
       Listener();
    }

    private void Listener()
    {
        TcpListener listener = new TcpListener(IPAddress.Any, 5000);
        listener.Start();

        while (offline == false)
        {
            TcpClient client = listener.AcceptTcpClient();
        }
    }
 }

Here is my remote object being constructed with an interface which is defined, it has one method which returns list.

This code is in the main of the console application, along with creating a new server instance.

        TcpChannel channel = new TcpChannel(5002);
        ChannelServices.RegisterChannel(channel, false);
        RemotingConfiguration.RegisterWellKnownServiceType(typeof(Server),
        "Server", WellKnownObjectMode.SingleCall);

        Server s = new Server();

This is how I construct the tcpchannel/remoting on my monitor

  IServer obj = (ServerInterface)Activator.GetObject(typeof(ServerInterface), "tcp://localhost:5002/Server");

but when I try to call one of it's method I get the error in the title,

  List<string> cons = obj.getIPs(); //error line

Any ideas guys?

Thank you.

War es hilfreich?

Lösung

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);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top