[转]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 on 2004-11-26 13:05 张逸 阅读(1570) 评论(2)  编辑 收藏 网摘 所属分类: .Net Remoting

评论

#1楼 2005-06-23 14:39 leogon

yun   回复  引用    

#2楼 2005-07-18 16:42 hnlzy[未注册用户]

怎么调用   回复  引用    




发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 69104




相关文章:

相关链接:

导航

公告

我的个人主页:
logo.gif
我的著作与译作

《软件设计精要与模式》

《WCF服务编程》

MVP_Horizontal_BlueOnly.png

From 03-03-2006
Counter: site stats
审批小组成员时,如有超过6个待审批成员,无法翻页察看。操作不便。

与我联系

搜索

 

常用链接

我参加的小组

我参与的团队

随笔分类(266)

随笔档案(258)

最新随笔

积分与排名

最新评论

阅读排行榜

评论排行榜