这是我的Hello远程处理应用程序。
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
我怎么才能解决这个问题?
发布于 2009-09-10 05:58:19
要么像这样更改服务器:
TcpChannel tcpChannel = new TcpChannel(8080);
或者像这样改变客户:
Activator.GetObject(typeof(MyInterface), "tcp://localhost:9999/FirstRemote");
在服务器端,您在指定的端口号上打开一个通道(在您的示例中,您使用的是端口9999)。本质上,这告诉服务器“监听”端口9999上的传入请求。在客户端,您告诉它要连接到哪个端口号(在您的示例中,您使用的是端口8080)。因此,您的服务器正在监听端口9999,而您的客户端正在尝试连接端口8080。这些端口号必须匹配。
发布于 2009-09-10 05:55:17
服务器tcpChannel为9999,客户端向8080请求
发布于 2009-09-10 05:56:09
当客户端查找8080时,您的服务器正在打开端口9999上的通道。
https://stackoverflow.com/questions/1405418
复制相似问题