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 ty开发者_C百科pe)
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!
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:
IDictionary properties = new Hashtable();
properties["port"] = 9932;
properties["bindTo"] = "192.168.1.141";
TcpServerChannel channel = new TcpServerChannel(properties, null);
ChannelServices.RegisterChannel(channel, false);
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.
精彩评论