Frage

Das ist mein Hallo Welt Remoting App.

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();
        }
    }
}

Aber nach dieser App ausgeführt wird, erhalte ich die folgende Ausnahme:

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

Wie kann ich dieses Problem beheben?

War es hilfreich?

Lösung

Sie entweder den Server wie folgt ändern:

TcpChannel tcpChannel = new TcpChannel(8080);

oder den Client wie folgt ändern:

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

Auf der Serverseite, sind Sie einen Kanal auf der angegebenen Port-Nummer zu öffnen (in Ihrem Beispiel verwenden Sie Port 9999). Im Wesentlichen teilen diese den Server zu ‚hören‘ für eingehende Anfragen auf Port 9999. Auf der Clientseite, sagen Sie ihm, welche Port-Nummer zu verbinden (in Ihrem Beispiel, Sie verwenden den Port 8080). Sie haben also eine Situation, wo der Server auf Port 9999 lauscht, aber Ihr Client versucht, auf Port 8080. Diese Portnummern übereinstimmen müssen angeschlossen werden.

Andere Tipps

Der Server TcpChannel ist 9999 die Client-Anfragen zu 8080

Der Server öffnet den Kanal auf Port 9999, während der Client für 8080 sucht.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top