Domanda

Questa è la mia app Hello World Remoting.

using System;
using System.Collections.Generic;
using System.Text;

namespace Remoting__HelloWorld.UI.Client
{
    public interface MyInterface
    {
        int FunctionOne(string str);
    }
}

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace Remoting__HelloWorld.UI.Client
{
    class MyClient
    {
        public static void Main()
        {
            TcpChannel tcpChannel = new TcpChannel();

            ChannelServices.RegisterChannel(tcpChannel);

            MyInterface remoteObj = (MyInterface) 
            Activator.GetObject(typeof(MyInterface), "tcp://localhost:8080/FirstRemote");

            Console.WriteLine(remoteObj.FunctionOne("Hello World!"));
        }
    }
}


using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using Remoting__HelloWorld.UI.Client;

namespace Remoting__HelloWorld.UI.Server
{
    public class MyRemoteClass : MarshalByRefObject, MyInterface
    {
        public int FunctionOne(string str)
        {
            return str.Length;
        }
    }
}


using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace Remoting__HelloWorld.UI.Server
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpChannel tcpChannel = new TcpChannel(9999);

            ChannelServices.RegisterChannel(tcpChannel);

            RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyRemoteClass), "FirstRemote", WellKnownObjectMode.SingleCall);

            System.Console.WriteLine("Press ENTER to quit");
            System.Console.ReadLine();
        }
    }
}

Ma dopo aver eseguito questa app, ottengo la seguente eccezione:

No connection could be made because the target machine 
actively refused it 127.0.0.1:8080

Come posso risolvere questo problema?

È stato utile?

Soluzione

O cambia il server in questo modo:

TcpChannel tcpChannel = new TcpChannel(8080);

o cambia il client in questo modo:

Activator.GetObject(typeof(MyInterface), "tcp://localhost:9999/FirstRemote");

Sul lato server, stai aprendo un canale sul numero di porta specificato (nel tuo esempio, stai usando la porta 9999). In sostanza, questo dice al server di "ascoltare" le richieste in arrivo sulla porta 9999. Sul lato client, gli indichi a quale numero di porta collegarti (nel tuo esempio, stai usando la porta 8080). Quindi hai una situazione in cui il tuo server è in ascolto sulla porta 9999, ma il tuo client sta provando a connettersi sulla porta 8080. Questi numeri di porta devono corrispondere.

Altri suggerimenti

il server tcpChannel è 9999 richiesto dal client verso 8080

Il tuo server sta aprendo il canale sulla porta 9999 mentre il client sta cercando 8080.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top