C# 通过TCP远程处理连接

C# 通过TCP远程处理连接,c#,timeout,remoting,C#,Timeout,Remoting,我已经建立了远程服务器和客户端。服务器设置如下: BinaryServerFormatterSinkProvider serverProv = new BinaryServerFormatterSinkProvider(); serverProv.TypeFilterLevel = TypeFilterLevel.Full; BinaryClientFormatterSinkProvider clientProv = new BinaryClientFormatterSink

我已经建立了远程服务器和客户端。服务器设置如下:

BinaryServerFormatterSinkProvider serverProv = 
    new BinaryServerFormatterSinkProvider();
serverProv.TypeFilterLevel = TypeFilterLevel.Full;
BinaryClientFormatterSinkProvider clientProv = 
    new BinaryClientFormatterSinkProvider();

IDictionary props = new Hashtable();
props["port"] = port;

TcpChannel channel = new TcpChannel( props, clientProv, serverProv );

ChannelServices.RegisterChannel( channel, false );

RemotingConfiguration.RegisterWellKnownServiceType( typeof( Controller ),
    "Controller", WellKnownObjectMode.Singleton );
ChannelServices.RegisterChannel( new TcpChannel( 0 ), false );
m_Controller = (Controller)RemotingServices.Connect( typeof( Controller ), 
      "tcp://" + ip + ":2594/Controller" );
客户机设置如下:

BinaryServerFormatterSinkProvider serverProv = 
    new BinaryServerFormatterSinkProvider();
serverProv.TypeFilterLevel = TypeFilterLevel.Full;
BinaryClientFormatterSinkProvider clientProv = 
    new BinaryClientFormatterSinkProvider();

IDictionary props = new Hashtable();
props["port"] = port;

TcpChannel channel = new TcpChannel( props, clientProv, serverProv );

ChannelServices.RegisterChannel( channel, false );

RemotingConfiguration.RegisterWellKnownServiceType( typeof( Controller ),
    "Controller", WellKnownObjectMode.Singleton );
ChannelServices.RegisterChannel( new TcpChannel( 0 ), false );
m_Controller = (Controller)RemotingServices.Connect( typeof( Controller ), 
      "tcp://" + ip + ":2594/Controller" );
当我尝试使用指定为“localhost”的IP从同一台计算机连接到服务器时,它连接良好。但是,当我尝试从远程计算机(而不是LAN)连接时,由于服务器的IP地址,我在长时间等待后在客户端收到以下异常:

A connection attempt failed because the connected party did not properly
respond after a period of time
我如何解决这个问题?我确信这是一个配置问题,因为端口在服务器上正确转发,并且没有防火墙处于活动状态


谢谢

这听起来好像服务器没有激活。使用localhost时,您是如何启动服务器的?是否使用远程IP地址执行了相同的操作?

从命令提示符cmd.exe,在服务器端运行netstat-a,只查看侦听行。在Local Address(本地地址)列中,应该有一个以冒号后面的数字显示服务器正在运行的端口:

>netstat -a
...
Proto  Local Address          Foreign Address        State
TCP    0.0.0.0:135            Blah:0                LISTENING
TCP    127.0.0.1:136          Blah:0                LISTENING
如果本地地址显示为127.0.0.1,则表示服务器应用程序仅在环回适配器上侦听。此适配器仅允许从本地计算机连接回自身,因此名称为,因此不接受到此类端口的远程连接。请注意,名称“localhost”是127.0.0.1的名称,因此,如果您在任何地方指定了该名称,这可能就是解释。上述示例中的136显示了侦听环回套接字

如果本地地址显示为0.0.0.0,则表示您的服务器应用程序正在侦听所有适配器,并且如果您有网络适配器,我将假定您有远程连接,则将接受该适配器。在上面的例子中是135

最后一种情况是,如果本地地址是另一个IP地址,则服务器应用程序正在侦听特定的网络适配器。如果您有多个适配器,则可能是它正在侦听错误的适配器

说到这里,我有一个小应用程序,上面有您发布的代码,默认情况下是在0.0.0.0所有适配器上侦听,因此如果您看到其他内容,我会感到惊讶。如果您确实看到了0.0.0.0,那么我的下一步工作将是查看客户端和服务器上的防火墙,如果您在一个组织中,则包括本地防火墙和公司防火墙


如果您有远程计算机的控制台访问权限,那么您可以尝试远程登录该端口上的本地计算机,查看它是否确实在侦听连接。我还想在服务器上替换一个简单的TCP侦听器,例如Netcat,以查看问题是否出在您的应用程序或基础结构上。

这个问题已经提出一个月了,但仍然存在:

嗅探服务器上的流量,最好是客户端上的流量,可以让您更清楚地了解发生了什么


对于我推荐的工作,一个非常友好的Windows数据包嗅探器。

看来您以前问过这个问题:我已经检查了所有这些,看起来很好。客户端可以在端口上远程登录。这一定是远程处理配置问题,但我似乎无法发现。请在您的答案下总结链接文本。如果这个链接断了,这个答案也会断。有关更多信息,请参阅。