[转]How to get the IP address of the Remoting Client on Remoting Server

In short, the Remoting Server can make use of Sink and Sink Provider to retrieve the IP address of the incoming request. The IP address is available in the Transport Headers of the incoming message. After retrieving the IP address in the Sink, we save the IP address to the CallContext, so that it will be available later in the code execution path as well. The followings are some background information regarding the topics:

Sinks and Sink Chains
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconsinkssinkchains.asp

Using CallContext
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconusingcallcontext.asp

Code modified from the sample by Ingo Rammer, author of “Advanced .Net Remoting”:

using System;

using System.Collections;

using System.IO;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Messaging ;

using System.Runtime.Remoting.Channels;

using System.Threading;

using System.Net;

 

namespace ClassLibRemotingIPSink

{

 

     public class ClientIPServerSinkProvider: IServerChannelSinkProvider

     {

         private IServerChannelSinkProvider next = null;

        

         public ClientIPServerSinkProvider(IDictionary properties, ICollection providerData)

         {

         }

        

         public void GetChannelData (IChannelDataStore channelData)

         {

         }

 

         public IServerChannelSink CreateSink (IChannelReceiver channel)

         {

              IServerChannelSink nextSink = null;

              if (next != null)

              {

                   nextSink = next.CreateSink(channel);

              }

              return new ClientIPServerSink(nextSink);

         }

 

         public IServerChannelSinkProvider Next

         {

              get { return next; }

              set { next = value; }

         }

     }

 

     public class ClientIPServerSink : BaseChannelObjectWithProperties, IServerChannelSink, IChannelSinkBase

     {   

         private IServerChannelSink _next;

 

         public ClientIPServerSink (IServerChannelSink next)

         {

              _next = next;

         }

 

         public void AsyncProcessResponse ( System.Runtime.Remoting.Channels.IServerResponseChannelSinkStack sinkStack , System.Object state , System.Runtime.Remoting.Messaging.IMessage msg , System.Runtime.Remoting.Channels.ITransportHeaders headers , System.IO.Stream stream )

         {

         }

 

         public Stream GetResponseStream ( System.Runtime.Remoting.Channels.IServerResponseChannelSinkStack sinkStack , System.Object state , System.Runtime.Remoting.Messaging.IMessage msg , System.Runtime.Remoting.Channels.ITransportHeaders headers )

         {

              return null;

         }

        

         public System.Runtime.Remoting.Channels.ServerProcessing ProcessMessage(System.Runtime.Remoting.Channels.IServerChannelSinkStack sinkStack, System.Runtime.Remoting.Messaging.IMessage requestMsg, System.Runtime.Remoting.Channels.ITransportHeaders requestHeaders, System.IO.Stream requestStream, out System.Runtime.Remoting.Messaging.IMessage responseMsg, out System.Runtime.Remoting.Channels.ITransportHeaders responseHeaders, out System.IO.Stream responseStream)

         {

              if (_next != null)

              {

                   IPAddress ip = requestHeaders[CommonTransportKeys.IPAddress] as IPAddress;

                   Console.WriteLine(ip.ToString());

                   CallContext.SetData("ClientIPAddress",ip);

                   ServerProcessing spres =  _next.ProcessMessage (sinkStack,requestMsg, requestHeaders,requestStream,out responseMsg,out responseHeaders,out responseStream);

                   return spres;

              }

              else

              {

                   responseMsg=null;

                   responseHeaders=null;

                   responseStream=null;

                   return new ServerProcessing();

              }

         }

  

         public IServerChannelSink NextChannelSink

         {

              get {return _next;}

              set {_next = value;}

         }

     }

}

posted @ 2004-11-26 13:05 张逸 阅读(1849) 评论(3)  编辑 收藏

 回复 引用   
#1楼 2005-06-23 14:39 leogon
yun
 回复 引用   
#2楼 2005-07-18 16:42 hnlzy[未注册用户]
怎么调用
 回复 引用 查看   
#3楼 2011-07-08 16:46 行者真无疆      
楼主:
我现在遇到个remoting的问题,场景是这样的:server采用单例,有一个事件,client可以订阅该事件,供server回调,正常情况下是实现功能了,但是当client有多个配置了IP地址的网络适配器时,server回调报异常:
System.Net.Sockets.SocketException: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。 169.254.16.247:1603

Server stack trace:
在 System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
在 System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
在 System.Runtime.Remoting.Channels.RemoteConnection.CreateNewSocket(EndPoint ipEndPoint)
在 System.Runtime.Remoting.Channels.RemoteConnection.CreateNewSocket()
在 System.Runtime.Remoting.Channels.RemoteConnection.GetSocket()
在 System.Runtime.Remoting.Channels.SocketCache.GetSocket(String machinePortAndSid, Boolean openNew)
在 System.Runtime.Remoting.Channels.Tcp.TcpClientTransportSink.SendRequestWithRetry(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream)
在 System.Runtime.Remoting.Channels.Tcp.TcpClientTransportSink.ProcessMessage(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream, ITransportHeaders& responseHeaders, Stream& responseStream)
在 System.Runtime.Remoting.Channels.BinaryClientFormatterSink.SyncProcessMessage(IMessage msg)

Exception rethrown at [0]:
在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
在 SubscribeProxy.SubscribeProxyService.HandleUpdated(Object sender, UpdatedEventArgs args)
在 Publisher.Publisher.OnUpdated(Object obj)
其中的169.254.16.247是client端不使用的一块网卡的IP地址。如果我把该不使用的网卡禁用就可以解决。
不知道您对该问题有没有更好的解决办法呢?或者在这种通过事件进行回调时,如何指定client端所使用的信道协议栈呢?被回调的是一个MarshalByRefObject类型的对象的方法,而不是client发布的remoting服务。