我們有一個服務器,我們用.Net遠程連接。.Net Remoting:指出使用哪個本地接口連接到一個服務器
服務器在兩個網絡上,客戶端在兩個網絡上。客戶端和服務器只有一個共同的網絡:
使用發現,我們找到服務器的IP(在本例中爲10.10.10.110)。我們創建了TcpChannel
,我們連接到服務器。
服務器收到呼叫,但是當它試圖發送一些信息給客戶端接收器時。我們得到一個例外,說我們試圖發送數據到一個不可升級的IP(10.12.10.100)。
因此,服務器正確地宣佈他的地址,但我們如何指示客戶端使用具有特定IP的網絡接口?
一些代碼:
客戶端,初始化:
IDictionary tcpChannelConfiguration = new Hashtable();
string instanceName = "RemotingClient" + Utils.GenerateRandomString(5);
tcpChannelConfiguration["name"] = instanceName);
tcpChannelConfiguration["port"] = 0;
tcpChannelConfiguration["machineName"] = m_interfaceToHost;//This is containing the local interface to use
tcpChannelConfiguration["bindTo"] = m_interfaceToHost;
IClientChannelSinkProvider formatClient = new BinaryClientFormatterSinkProvider(tcpChannelConfiguration, null);
IClientChannelSinkProvider identityFormatClient = new IdentityClientSinkProvider{Next = formatClient};
BinaryServerFormatterSinkProvider formatServer = new BinaryServerFormatterSinkProvider(tcpChannelConfiguration, null)
{TypeFilterLevel = TypeFilterLevel.Full};
m_channel = new TcpChannel(tcpChannelConfiguration, identityFormatClient, formatServer);
ChannelServices.RegisterChannel(m_channel, false);
//Then we get the remote object:
IServer server = (IServer)Activator.GetObject(typeof(IServer), String.Format("tcp://{0}:{1}/{2}", m_ipAddress, m_port, instanceName));
[...]
下面是我在服務器端異常:
System.Net.Sockets.SocketException (0x80004005): A socket operation was attempted to an unreachable network 10.12.10.100:54330
Server stack trace:
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress 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 machinePortAndSid, Boolean openNew)
at System.Runtime.Remoting.Channels.Tcp.TcpClientTransportSink.SendRequestWithRetry(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream)
at System.Runtime.Remoting.Channels.Tcp.TcpClientTransportSink.ProcessMessage(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream, ITransportHeaders& responseHeaders, Stream& responseStream)
at System.Runtime.Remoting.Channels.BinaryClientFormatterSink.SyncProcessMessage(IMessage msg)
我怎麼能指示客戶端的IP /它必須提供給服務器的接口?
這是我們可以通過給自定義ClientSinkProvider
嗎?
編輯
我發現了一些可能被interessting,我注意到,對於簡單的查詢和響應,在.NET遠程工作正常,但對於其中的一些,查詢給出了一個對象,這將是用作服務的回調,在這種情況下,它不起作用。
我剛剛得到.Net的源代碼來檢查這是如何完成的,但是我沒有發現它是爲相反方向創建的代理。
雖然擁有TcpChannel的源代碼,但我發現在某些時候,服務器端(它將建立與客戶端回調的連接)的方法接收到帶有正確遠程IP的請求但結尾的URI中有錯誤的IP:
您是否嘗試添加'bindTo'設置? https://msdn.microsoft.com/en-us/library/ms973907.aspx – rene
@rene是的,但作爲spec狀態:「此屬性只能在服務器端使用。」 – J4N
好吧,這可能有點破解,但如何在服務器上添加路由到路由表以便爲該IP使用特定的NIC?僅用於測試來驗證它只是一個路由問題。 – rene