Winform獲取本地IP和外網IP

2022-09-06 00:06:21 字數 676 閱讀 5739

一.本地ip

winform要獲得本地ip,就要想到c#中的 iphostentry 類

好吧,方法如下:

引入命名空間:

using

system.net;

實現方法:

iphostentry iphost 

=dns.resolve(dns.gethostname()); ;

ipaddress ipaddress 

=iphost.addresslist[0];

string

ips 

=ipaddress.tostring();

二.外網ip

winform想獲得外網ip,類庫里好像還沒有這樣乙個類哦,就只好去訪問返回ip資訊的網頁來讀取,目前我只找到這個方法.

我找了兩個網頁提供給大家哈

方法如下:

//////

獲得客戶端外網ip位址

//////

ip位址

public

static

string

getclientinternetip()

\\.[0-9]\\.[0-9]\\.[0-9]"))

}return

ipaddress;

}

c 獲取本地ip和外網ip

ctest.cpp 此檔案包含 main 函式。程式執行將在此處開始並結束。include include include define max size 1024 pragma comment lib,urlmon.lib pragma comment lib,ws2 32.lib include...

獲取本地IP

bool socketuti getlocalipaddr string stripaddr if gethostname myname,80 socket error return false if thishost gethostbyname myname return false memset...

獲取本地IP

本機可能會有多個ip,下面這段程式可以獲得本機ip struct hostent h char host 100 int i gethostname host,100 if h gethostbyname host null printf error s n hstrerror h errno re...