Obtain the external IP address of the local machine in the LAN

Source: Internet
Author: User

“Ipconfig.exe/all "can only obtain internal IP addresses in the LAN, and cannot obtain external IP addresses of the local machine, as does DNS. Resolve.

After research, in the LAN, only by the power of the local machine is unable to obtain the external IP of the local machine, you must log on to a server on the Internet, that server will know your IP address, and then you get it back.

1 Using System;
2 Using System. IO;
3 Using System. net;
4 Using System. Text. regularexpressions;
5
6 Class Test
7 {
8 // Obtain the HTML content of a webpage
9 Static   String Getpage ( String URL)
10 {
11 Httpwebrequest req = (Httpwebrequest) webrequest. Create (URL );
12 Try
13 {
14 Using (Httpwebresponse res = (Httpwebresponse) Req. getresponse ())
15 {
16 Using (Streamreader SR =   New Streamreader (res. getresponsestream ()))
17 {
18ReturnSr. readtoend ();
19}
20 }
21 }
22 Catch (System. Exception E)
23 {
24ReturnE. message;
25}
26 Finally
27 {
28Req. Abort ();
29}
30 }
31
32 // Obtain the external IP address of the local machine through an external website
33 Static   String Getouterip ()
34 {
35 String Patt =   @" IP :\[(? <Ip> [0-9 \.] *) \] " ;
36 String URL =   " Http://www.skyiv.com/info " ;
37 Return RegEx. Match (getpage (URL), patt). Groups [ " IP " ]. Value;
38 }
39
40 Static   Void Main ()
41 {
42 Console. writeline ( " Inner IP: " );
43 Foreach (IPaddress IP In DNS. Resolve (DNS. gethostname (). Addresslist)
44 {
45Console. writeline (IP );
46}
47 Console. writeline ();
48 Console. writeline ( " Outer IP: " );
49 Console. writeline (getouterip ());
50 }
51 }

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.