Question

Ceci est mon application 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();
        }
    }
}

Mais après avoir exécuté cette application, j'obtiens l'exception suivante:

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

Comment puis-je résoudre ce problème?

Était-ce utile?

La solution

Changez le serveur comme suit:

TcpChannel tcpChannel = new TcpChannel(8080);

ou changez le client comme ceci:

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

Du côté serveur, vous ouvrez un canal sur le numéro de port spécifié (dans votre exemple, vous utilisez le port 9999). En gros, cela indique au serveur d'écouter les demandes entrantes sur le port 9999. Côté client, vous lui indiquez le numéro de port auquel vous souhaitez vous connecter (dans votre exemple, vous utilisez le port 8080). Votre serveur écoute donc sur le port 9999 alors que votre client tente de se connecter sur le port 8080. Ces numéros de port doivent correspondre.

Autres conseils

le serveur tcpChannel correspond à 9999 demandes du client vers 8080

Votre serveur ouvre le canal sur le port 9999 alors que le client recherche 8080.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top