.NET远程处理HelloWorld
这是我的Hello World远程应用程序.NET远程处理HelloWorld,.net,remoting,.net,Remoting,这是我的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.Runti
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
如何修复此问题?服务器TCPCchannel是9999客户端向8080请求时当客户端正在查找8080时,您的服务器正在打开端口9999上的频道。可以这样更改服务器:
TcpChannel tcpChannel = new TcpChannel(8080);
Activator.GetObject(typeof(MyInterface), "tcp://localhost:9999/FirstRemote");
或者像这样更改客户端:
TcpChannel tcpChannel = new TcpChannel(8080);
Activator.GetObject(typeof(MyInterface), "tcp://localhost:9999/FirstRemote");
在服务器端,您正在指定的端口号上打开一个通道(在您的示例中,您使用的是端口9999)。本质上,这告诉服务器“侦听”端口9999上的传入请求。在客户端,您告诉它要连接到哪个端口号(在您的示例中,您使用的是8080端口)。因此,您的服务器正在侦听端口9999,而您的客户端正在尝试连接端口8080。这些端口号必须匹配