How to obtain the Client IP address through WCF

Source: Internet
Author: User

When I first learned about WCF, I remembered that I could only get the client's intranet IP address. Now I forgot about it and I don't know how to get it.

Iphostentry v = system. net. DNS. Resolve (system. net. DNS. gethostname ());

Console. writeline (system. net. DNS. resolve (system. net. DNS. gethostname ()). addresslist. getvalue (0 ). tostring ());//. current. request. userhostaddress;


String hostname = system. net. DNS. gethostname ();
Iphostentry
Iphostentry = system. net. DNS. gethostentry (hostname );
All these obtained Intranet IP addresses, but the IP address used by the client is unknown when multiple NICs exist.

Please kindly advise! Thank you very much!

 

The following is an Internet IP address, which has not been found for a long time.

// Context for method execution

Operationcontext context = operationcontext. Current;
// Obtain the attributes of the sent message

Messageproperties properties = context. incomingmessageproperties;
// Obtain the IP address and port of the remote endpoint for message sending.

Remoteendpointmessageproperty endpoint = properties [remoteendpointmessageproperty. Name]
As remoteendpointmessageproperty;


Console. writeline (string. Format ("Hello {0}, you are from
{1 }:{ 2} Intranet IP = {3} ", name, endpoint. Address,
Endpoint. Port

 

 

Try using the following code.

Using system. servicemodel;
 
Using system. servicemodel. channels;
 
Operationcontext context = operationcontext. Current;
 
Messageproperties prop = context. incomingmessageproperties;
 
Remoteendpointmessageproperty endpoint = prop [remoteendpointmessageproperty. Name] As remoteendpointmessageproperty;
 
String IP = endpoint. address;
 
 
 
Namespace sample
 
{
 
Using system;
 
Using system. servicemodel;
 
Using system. servicemodel. channels;
 
Using system. net;
 
 
 
[Servicecontract]
 
Interface imyservice
{
 
[Operationcontract]
 
Public String processuntyped (message );
 
[Operationcontract]
 
Public String processtyped (string Str );
 
}
 
 
 
Class myservice: imyservice
 
{
 
Public String processuntyped (message)
 
{
Remoteendpointmessageproperty endpoint = message. properties [remoteendpointmessageproperty. Name] As remoteendpointmessageproperty;
 
Return string. Format ("connected from {0 }:{ 1}", STR, endpoint. Address, endpoint. Port );
 
}
 
 
 
Public String processtyped (string Str)
 
{
 
Operationcontext context = operationcontext. Current;
 
Messageproperties properties = context. incomingmessageproperties;
Remoteendpointmessageproperty endpoint = properties [remoteendpointmessageproperty. Name] As remoteendpointmessageproperty;
 
Return string. Format ("You said: '{0}'. connected from {1 }:{ 2}", STR, endpoint. Address, endpoint. Port );
 
}
 
}
 
}

Obtain the IP address of the clientCode:

 
/*************************************** **************************************** ******
 
* Code: Wu Jiang
 
* Time: 2012.02.07
 
* Description: Security
 
* Others:
 
* Modifier:
 
* Modification time:
 
* Modification description:
**************************************** **************************************** ****/
 
Using system. servicemodel;
 
Using system. servicemodel. channels;
 
 
 
Namespace tools
 
{
 
Public class safe
 
{
 
Public static safe instance ()
 
{
 
Return new safe ();
 
}
 
 
 
Public String clientip ()
{
 
Operationcontext context = operationcontext. Current;
 
Messageproperties properties = context. incomingmessageproperties;
 
Remoteendpointmessageproperty endpoint = properties [remoteendpointmessageproperty. Name] As remoteendpointmessageproperty;
 
Return endpoint. address;
 
}
 
 
 
Public String clientport ()
{
 
Operationcontext context = operationcontext. Current;
 
Messageproperties properties = context. incomingmessageproperties;
 
Remoteendpointmessageproperty endpoint = properties [remoteendpointmessageproperty. Name] As remoteendpointmessageproperty;
 
Return endpoint. Port. tostring ();
 
}
 
 
 
Public String clientipandport ()
 
{
Operationcontext context = operationcontext. Current;
 
Messageproperties properties = context. incomingmessageproperties;
 
Remoteendpointmessageproperty endpoint = properties [remoteendpointmessageproperty. Name] As remoteendpointmessageproperty;
 
Return endpoint. Address + ";" + endpoint. Port. tostring ();
 
}
 
}
 
}
 
 

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.