在两台计算机之间进行 .NET 远程处理时出现套接字错误
对于我们正在进行的一个项目,我们使用 .NET 远程处理在服务器和客户端之间进行通信。到目前为止,我们已经在同一台机器上使用本地主机和任意端口号对其进行了测试并且它可以正常工作。但是,当在同一网络上的两台机器之间进行测试时,主要组件可以工作,但代理对象会失败。
我们使用配置文件来设置服务器名称和端口,如下所示:
<setting name="ServerName" serializeAs="String">
<value>192.168.1.141</value>
</setting>
<setting name="Port" serializeAs="String">
<value>9932</value>
</setting>
客户端按如下方式设置远程处理对象:
_port = global::ConsoleClient.Properties.Settings.Default.Port;
_servername = global::ConsoleClient.Properties.Settings.Default.ServerName;
string url = "tcp://" + _servername + ":" + _port + "/Lobby";
try
{
ChannelServices.RegisterChannel(new TcpClientChannel(), false);
Lobby lobby = (Lobby)Activator.GetObject(
typeof(Lobby), url);
}
catch (Exception e)
{
}
这工作正常,我们可以调用大厅对象上的方法。然而,稍后在代码中,我们返回另一个对象,表如下:
table_proxy = lobby_proxy.joinTable(userID, i);
其中lobby_proxy 与之前代码中的大厅对象相同。服务器按以下方式配置:
_port = global::Server.Properties.Settings.Default.Port;
_lobby = new Lobby(_db);
try
{
TcpServerChannel channel = new TcpServerChannel(_port);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Lobby),
"Lobby", WellKnownObjectMode.Singleton);
RemotingServices.Marshal((_lobby), "Lobby");
System.Console.WriteLine("Press Any Key");
}
catch (Exception e)
{
System.Console.WriteLine(e);
}
在同一台计算机上运行时工作正常,但在单独的计算机上运行时会抛出以下错误:
System.Net.Sockets.SocketException (0x80004005): A socket operation was attempte
d to an unreachable network 169.254.171.86:9932
Exit
Server stack trace:
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre
ss socketAddress)
at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
at System.Runtime.Remoting.Channels.RemoteConnection.CreateNewSocket(EndPoint
ipEndPoint)
at System.Runtime.Remoting.Channels.RemoteConnection.CreateNewSocket()
at System.Runtime.Remoting.Channels.RemoteConnection.GetSocket()
at System.Runtime.Remoting.Channels.SocketCache.GetSocket(String machinePortA
ndSid, Boolean openNew)
at System.Runtime.Remoting.Channels.Tcp.TcpClientTransportSink.SendRequestWit
hRetry(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream)
at System.Runtime.Remoting.Channels.Tcp.TcpClientTransportSink.ProcessMessage
(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream, ITranspor
tHeaders& responseHeaders, Stream& responseStream)
at System.Runtime.Remoting.Channels.BinaryClientFormatterSink.SyncProcessMess
age(IMessage msg)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage req
Msg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgDa
ta, Int32 type)
at JackOfTrades.Common.Table.GetTableData(Boolean full)
查看顶部,有一个我不认识的 IP 地址,它不在网络或路由器的 IP。
有什么问题的想法吗?帮助将不胜感激!
For a project we're working on, we're using .NET remoting to communicate between the server and client. So far we've been testing it on the same machine and its worked, using localhost and an arbitrary port number. However, when testing between two machines on the same network, using, the main component works, but a proxy object fails.
We're using configuration files to set the server name and port, like this:
<setting name="ServerName" serializeAs="String">
<value>192.168.1.141</value>
</setting>
<setting name="Port" serializeAs="String">
<value>9932</value>
</setting>
The client sets up the remoting object as follows:
_port = global::ConsoleClient.Properties.Settings.Default.Port;
_servername = global::ConsoleClient.Properties.Settings.Default.ServerName;
string url = "tcp://" + _servername + ":" + _port + "/Lobby";
try
{
ChannelServices.RegisterChannel(new TcpClientChannel(), false);
Lobby lobby = (Lobby)Activator.GetObject(
typeof(Lobby), url);
}
catch (Exception e)
{
}
This works fine and we are able to call methods on the lobby object. However, later on in the code, we return another object, table as follows:
table_proxy = lobby_proxy.joinTable(userID, i);
where lobby_proxy is the same lobby object in the prior code. The server is configured in the following manner:
_port = global::Server.Properties.Settings.Default.Port;
_lobby = new Lobby(_db);
try
{
TcpServerChannel channel = new TcpServerChannel(_port);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Lobby),
"Lobby", WellKnownObjectMode.Singleton);
RemotingServices.Marshal((_lobby), "Lobby");
System.Console.WriteLine("Press Any Key");
}
catch (Exception e)
{
System.Console.WriteLine(e);
}
This works fine when run on the same machine, but throws the following error when run on separate machines:
System.Net.Sockets.SocketException (0x80004005): A socket operation was attempte
d to an unreachable network 169.254.171.86:9932
Exit
Server stack trace:
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre
ss socketAddress)
at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
at System.Runtime.Remoting.Channels.RemoteConnection.CreateNewSocket(EndPoint
ipEndPoint)
at System.Runtime.Remoting.Channels.RemoteConnection.CreateNewSocket()
at System.Runtime.Remoting.Channels.RemoteConnection.GetSocket()
at System.Runtime.Remoting.Channels.SocketCache.GetSocket(String machinePortA
ndSid, Boolean openNew)
at System.Runtime.Remoting.Channels.Tcp.TcpClientTransportSink.SendRequestWit
hRetry(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream)
at System.Runtime.Remoting.Channels.Tcp.TcpClientTransportSink.ProcessMessage
(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream, ITranspor
tHeaders& responseHeaders, Stream& responseStream)
at System.Runtime.Remoting.Channels.BinaryClientFormatterSink.SyncProcessMess
age(IMessage msg)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage req
Msg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgDa
ta, Int32 type)
at JackOfTrades.Common.Table.GetTableData(Boolean full)
Looking at the top, there is an IP address that I do not recognize, it's neither on the network nor the IP of the router.
Any thoughts on whats wrong? Help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用以下参数设置 TcpServerChannel:
bindTo
指定服务器通道应绑定到的网络接口卡 (NIC) IP 地址的字符串。默认值为 System.Net.IPAddress.Any。
http://msdn.microsoft.com/en-us/library/bb397831.aspx
更新:
You need to setup TcpServerChannel with following parameter:
bindTo
A string that specifies the IP address of the network interface card (NIC) to which the server channel should bind. The default value is System.Net.IPAddress.Any.
http://msdn.microsoft.com/en-us/library/bb397831.aspx
UPDATED:
当网络适配器无法获取 DHCP 地址时,通常会看到 168.254.xx 地址。
我会检查以确保您运行此代码的计算机具有有效的 IP 地址。
A 168.254.x.x address is usually seen when a network adapter cannot get a DHCP address.
I would check to make sure that the machine you are running this code on has a valid IP address.