.NET 远程处理 HelloWorld

发布于 2024-08-04 04:59:46 字数 1960 浏览 4 评论 0原文

这是我的 Hello World 远程应用程序。

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

如何解决此问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

朱染 2024-08-11 04:59:46

服务器 tcpChannel 是 9999 客户端请求到 8080

the server tcpChannel is 9999 the client requests towards 8080

猫性小仙女 2024-08-11 04:59:46

您的服务器正在端口 9999 上打开通道,而客户端正在寻找 8080。

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

我家小可爱 2024-08-11 04:59:46

要么像这样更改服务器:

TcpChannel tcpChannel = new TcpChannel(8080);

要么像这样更改客户端:

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

在服务器端,您在指定的端口号上打开一个通道(在您的示例中,您使用端口 9999)。本质上,这告诉服务器“侦听”端口 9999 上的传入请求。在客户端,您告诉它要连接到的端口号(在您的示例中,您使用的是端口 8080)。因此,您会遇到这样的情况:您的服务器正在侦听端口 9999,但您的客户端正在尝试在端口 8080 上进行连接。这些端口号必须匹配。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文