Remoting get the client's extranet IP address

Source: Internet
Author: User

In fact, the server remoting can be like reading a variable to get to the IP of the connected client, but this IP is not too easy to take.

There is a solution on the web to use the configuration file to suck the IP address, but this scenario requires us to use the configuration file to use remoting, if we are the code form of the program, then how to obtain the client IP.

Remoting, WebService bottom are all with sockets to achieve, the socket itself is easy to obtain IP address, so theoretically Remoting get client IP is through.

We can get from the suction device, if we use the TCP channel to communicate, in the service side using the TcpServerChannel class, the class has actually supported the extractors, the constructor is as follows:

Public TcpServerChannel (string name, int port, IServerChannelSinkProvider sinkprovider);

The TcpServerChannel class defaults to the BinaryServerFormatterSinkProvider, and the picker can set the next picker on the list, then execute each one in turn.

We can get the following code:

IServerChannelSinkProvider Serversink = new Clientipserversinkprovider (); BinaryServerFormatterSinkProvider Firstsink = new BinaryServerFormatterSinkProvider (); firstSink.Next = ServerSink; TcpServerChannel channel = new TcpServerChannel ("TCPServer", port, Firstsink); ChannelServices.RegisterChannel (channel, true); RemotingConfiguration.RegisterWellKnownServiceType (typeof (Remotableclass), "Remotableclass", Wellknownobjectmode.singleton);

Here, if the Remotableclass class is defined, then you can take the IP address directly:

public class Remotableclass:marshalbyrefobject {protected String getipaddress () {System.Net.IPAddress IP = System.Runtime.Remoting.Messaging.CallContext.GetData ("Clientipaddress") as System.net.ipaddress;if (IP! = null) { return IP. ToString ();} return null;}}

If you do not know the code of the suction device, you can refer to the following:

public class clientipserversinkprovider:iserverchannelsinkprovider{private IServerChannelSinkProvider next = null; Public Clientipserversinkprovider () {}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 (Iserverresponsechannelsinkstack sinkstack, Object State, IMessage msg, ITransportHeaders headers, Stream stream) {}public stream GetResponseStream (Iserverresponsechannelsinkstack sinkstack ObjECT State, IMessage msg, itransportheaders headers) {return null;} Public System.Runtime.Remoting.Channels.ServerProcessing ProcessMessage (Iserverchannelsinkstack sinkstack, IMessage requestmsg, ITransportHeaders requestheaders, Stream requeststream, out IMessage responsemsg, out ITransportHeaders responseheaders, out Stream responsestream) {if (_next! = null) {IPAddress IP = requestheaders[ Commontransportkeys.ipaddress] as IPAddress; Callcontext.setdata ("clientipaddress", IP); return _next. ProcessMessage (Sinkstack, Requestmsg, Requestheaders, Requeststream, out responsemsg, out responseheaders, out Responsestream);} else{responsemsg = Null;responseheaders = Null;responsestream = Null;return serverprocessing.complete;}} Public IServerChannelSink Nextchannelsink{get{return _next;} Set{_next = value;}}}

It's done here.

Remoting get the client's extranet IP address

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.