.NET 远程处理 HelloWorld
这是我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
服务器 tcpChannel 是 9999 客户端请求到 8080
the server tcpChannel is 9999 the client requests towards 8080
您的服务器正在端口 9999 上打开通道,而客户端正在寻找 8080。
Your server is opening the channel on port 9999 while the client is looking for 8080.
要么像这样更改服务器:
要么像这样更改客户端:
在服务器端,您在指定的端口号上打开一个通道(在您的示例中,您使用端口 9999)。本质上,这告诉服务器“侦听”端口 9999 上的传入请求。在客户端,您告诉它要连接到的端口号(在您的示例中,您使用的是端口 8080)。因此,您会遇到这样的情况:您的服务器正在侦听端口 9999,但您的客户端正在尝试在端口 8080 上进行连接。这些端口号必须匹配。
Either change the server like this:
or change the client like this:
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.