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 ();
}
}
}