.NET Remoting HelloWorld

江枫思渺然 提交于 2019-12-12 05:31:31

问题


This is my Hello World 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();
        }
    }
}

But after running this app, I am getting the following Exception:

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

How can I fix this?


回答1:


Either change the server like this:

TcpChannel tcpChannel = new TcpChannel(8080);

or change the client like this:

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

On the server side, you're opening a channel on the specified port number (in your example, you're using port 9999). In essence, this tells the server to 'listen' for incoming requests on port 9999. On the client side, you tell it what port number to connect to (in your example, you're using port 8080). So you have a situation where your server is listening on port 9999, but your client is trying to connect on port 8080. These port numbers must match.




回答2:


the server tcpChannel is 9999 the client requests towards 8080




回答3:


Your server is opening the channel on port 9999 while the client is looking for 8080.



来源:https://stackoverflow.com/questions/1405418/net-remoting-helloworld

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!