C # Get intranet IP, extranet IP, client IP method

Source: Internet
Author: User

Objective

In Windows Form application for the IP address has intranet, extranet two IP address, if only need to obtain the intranet IP address, can be obtained through the use of the Iphostentry category, the external network must be sent A Request to a website that can obtain the IP address and then analyze the content of Response, the site can do it yourself or use off-the-shelf, such as http://www.whatismyip.com.tw/.

In addition, in the case of using Socket communication, there may also be need to know the client to connect the IP address is what, then you can use the Socket.remoteendpoint property to obtain the remote IP address and communication port number, Here's a list of sample code to see how to use it.

Sample program code

/// <summary>        ///get extranet IP address/// </summary>        /// <returns></returns>        Private stringgetextranetipaddress () {HttpWebRequest request= Httpwebrequest.create ("http://www.whatismyip.com.tw") asHttpWebRequest; Request. Method="GET"; Request. ContentType="application/x-www-form-urlencoded"; Request. UserAgent="mozilla/5.0"; stringIP =string.            Empty; WebResponse response=request.            GetResponse (); using(StreamReader reader =NewStreamReader (response. GetResponseStream ())) {stringresult =Reader.                ReadToEnd (); stringPattern =@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"; IP=Regex.match (result, pattern).            ToString (); }            returnip//result:210.125.21.xxx}
get extranet IP address
/// <summary>        ///get the native IP address/// </summary>        /// <returns></returns>        Privatelist<string>gethostipaddress () {List<string> lstipaddress =Newlist<string>(); Iphostentry Ipentry=Dns.gethostentry (Dns.gethostname ()); foreach(IPAddress IPAinchipentry.addresslist) {if(IPA. AddressFamily = =addressfamily.internetwork) lstipaddress.add (IPA.            ToString ()); }            returnlstipaddress;//result:192.168.1.17 ...}
get the native IP address

/// <summary>///Perform server monitoring/// </summary> Public Static voidRunserver () {//Establish X509 credentialsServercertificate =NewX509Certificate (Certificate,"SSL"); //listen to any message from the IP addressListener =NewTcpListener (System.Net.IPAddress.Any,17170); //Turn on monitoringListener.    Start ();  while(isrun) {TcpClient client=Listener.        AcceptTcpClient ();    Processclient (client); }}/// <summary>///receive client message processing and reply/// </summary>/// <param name= "pclient" ></param>Private Static voidprocessclient (TcpClient pclient) {SslStream SslStream=NewSslStream (Pclient.getstream (),true); //get the connector IP with PortIPEndPoint point = PClient.Client.RemoteEndPoint asIPEndPoint; stringIP = point. Address.tostring ();//result:127.0.0.1    stringPort = point. Port.tostring ();//result:55236//Do something ...}
Socket gets remote IP address and Port

string tempip =string. Empty; if (System.Net.Dns.GetHostEntry (System.Net.Dns.GetHostName ()). Addresslist.length >1)     Tempip = System.Net.Dns.GetHostEntry (System.Net.Dns.GetHostName ()). addresslist[1]. ToString ();
if it is ADSL Internet access, get the public network IP

C # Get intranet IP, extranet IP, client IP method

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.