伺服器獲取本地Ip位址

2021-06-15 02:03:05 字數 522 閱讀 9935

string getlocalip()

; char buff[bufsiz];

struct ifconf conf;

conf.ifc_len = bufsiz;

conf.ifc_buf = buff;

const int s = socket(pf_inet, sock_dgram, 0);

ioctl(s, siocgifconf, &conf);

const int num = conf.ifc_len / sizeof(struct ifreq);

const struct ifreq *ifr = conf.ifc_req;

for(int cnt = 0 ; cnt < num; ++cnt)

ifr++;

}::close(s);

return string(ip);

}

在網上很多獲取本地ip的方法會造成fd的洩露,原因是它沒有關閉fd,即:::close(s)。

獲取本地Mac位址,伺服器IP,及驗證

獲取本地mac位址 public string getmac using system.management.managementclass mc new system.management.managementclass win32 networkadapterconfiguration syst...

獲取伺服器ip,本地ip,本在mac

using system.net 在此處放置使用者 以初始化頁面 string strhostname dns.gethostname 得到本機的主機名 iphostentry ipentry dns.gethostbyname strhostname 取得本機ip sessionid sessio...

java獲取本地IP和伺服器IP

string address inetaddress.getlocalhost gethostaddress tostring 這種方法能不能在linux伺服器上直接獲取ip有待驗證 設定異常資訊 param t 異常資訊。注意 這裡使用的是throwable來接收引數,為什麼不是使用excepti...