質問

これは私の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();
        }
    }
}

しかし、このアプリを実行すると、次の例外が発生します:

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

これを修正するにはどうすればよいですか

役に立ちましたか?

解決

次のようにサーバーを変更します:

TcpChannel tcpChannel = new TcpChannel(8080);

または次のようにクライアントを変更します:

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

サーバー側では、指定したポート番号でチャネルを開いています(この例では、ポート9999を使用しています)。本質的に、これはサーバーにポート9999で着信要求を「リッスン」するように指示します。クライアント側では、接続するポート番号を指定します(この例では、ポート8080を使用しています)。サーバーがポート9999でリッスンしているが、クライアントがポート8080で接続しようとしている状況があります。これらのポート番号は一致する必要があります。

他のヒント

サーバーtcpChannelは9999であり、クライアントは8080に向かって要求します

クライアントが8080を探している間、サーバーはポート9999でチャネルを開いています。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top