C/C++ 獲取自身IP與域名片段的示例代碼
判斷大端序小端序:
通常情況下,數(shù)值在內(nèi)存中存儲的方式有兩種,一種是大尾字節(jié)序,另一種是小尾,比如0x01020304這樣一個(gè)數(shù)值,如果用大尾方式存儲,其存儲方式為01 02 03 04而用小尾方式存儲則是04 03 02 01,一般Windows操作系統(tǒng)兼容的CPU為小尾方式,而UNIX操作系統(tǒng)所兼容的CPU多為大尾方式,通過使用兩種方法即可判斷大小緯。
#include <stdio.h>
#include <stdlib.h>
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
// 變量法判斷
void GetA()
{
DWORD dwSmallNum = 0x01020304;
if (*(BYTE *)&dwSmallNum == 0x04)
printf("小端字節(jié)序 \n");
else
printf("大端字節(jié)序 \n");
}
// 直接轉(zhuǎn)換法判斷
void GetB()
{
DWORD dwSmallNum = 0x01020304;
if (dwSmallNum == htonl(dwSmallNum))
printf("大端字節(jié)序 \n");
else
printf("小端字節(jié)序 \n");
}
int main(int argc, char *argv[])
{
GetA();
GetB();
system("pause");
return 0;
}
利用域名獲取IP:
通過使用winsocket庫中的gethostbyname()可以將一個(gè)域名所對應(yīng)的別名,地址類型等信息提取出來。
#include <stdio.h>
#include <stdlib.h>
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
BOOL GetHostByName(char * HostName)
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
return FALSE;
struct hostent *ptr = gethostbyname(HostName);
if (!ptr){ return FALSE; }
printf("地址類型: %s \n", (ptr->h_addrtype == AF_INET) ? "IPV4" : "IPV6");
for (int i = 0; ptr->h_aliases[i]; i++){
printf("別名 [%d]: %s \n", i + 1, ptr->h_aliases[i]);
}
for (int i = 0; ptr->h_addr_list[i]; i++){
printf("IP地址 [%d]: %s \n", i + 1, inet_ntoa(*(struct in_addr*)ptr->h_addr_list[i]));
}
WSACleanup();
return TRUE;
}
int main(int argc ,char *argv[])
{
GetHostByName("www.baidu.com");
system("pause");
return 0;
}
取自身主機(jī)名IP地址:
有時(shí)候我們需要得到自身IP地址,這里我封裝了兩種獲取IP地址的方法。
#include <stdio.h>
#include <stdlib.h>
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
char * GetLocalHostName()
{
WSADATA wsaData;
HOSTENT *pHost;
char szHostName[256];
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
exit(0);
if (gethostname(szHostName, 256) == 0)
{
char *Host;
Host = (char *)malloc(1024);
pHost = gethostbyname(szHostName);
strcpy(Host, pHost->h_name);
return Host;
}
return "";
}
char * GetLocalHostAddr(int Count)
{
WSADATA wsaData;
HOSTENT *pHost;
char szHostName[256];
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
exit(0);
if (gethostname(szHostName, 256) == 0)
{
char tmp[15];
char *Addr;
pHost = gethostbyname(szHostName);
int index = 0;
for (; index < 10; index++)
{
if (pHost->h_addr_list[index] == NULL)
break;
}
sprintf(tmp, "%d.%d.%d.%d",
pHost->h_addr_list[0][0] & 0xff,
pHost->h_addr_list[0][1] & 0xff,
pHost->h_addr_list[0][2] & 0xff,
pHost->h_addr_list[0][3] & 0xff);
Addr = (char *)malloc(15);
strcpy(Addr, tmp);
return Addr;
}
return "";
}
int main(int argc, char *argv[])
{
char *hostname = GetLocalHostName();
printf("本機(jī)名字: %s \n", hostname);
char *hostaddr = GetLocalHostAddr(0);
printf("本機(jī)IP: %s \n", hostaddr);
system("pause");
return 0;
}
以上就是C/C++ 獲取自身IP與域名片段的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C/C++ 獲取自身IP與域名片段的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解C++ STL模擬實(shí)現(xiàn)forward_list
forward_list是C++ 11新增的容器,它支持從容器中的任何位置快速插入和移除元素的容器,不支持快速隨機(jī)訪問。本文將模擬實(shí)現(xiàn)forward_list,感興趣的可以了解一下2023-01-01
C++ 動(dòng)態(tài)創(chuàng)建按鈕及 按鈕的消息響應(yīng)
這篇文章主要介紹了C++ 動(dòng)態(tài)創(chuàng)建按鈕及 按鈕的消息響應(yīng)的相關(guān)資料,需要的朋友可以參考下2015-06-06
C語言實(shí)現(xiàn)餐飲結(jié)賬管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)餐飲結(jié)賬管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
C++ 模擬實(shí)現(xiàn)list(迭代器)實(shí)現(xiàn)代碼
這篇文章主要介紹了C++ 模擬實(shí)現(xiàn)list(迭代器)實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05
淺談C++中的string 類型占幾個(gè)字節(jié)
本篇文章小編并不是為大家講解string類型的用法,而是講解我個(gè)人比較好奇的問題,就是string 類型占幾個(gè)字節(jié)2013-08-08
c++中為什么可以通過指針或引用實(shí)現(xiàn)多態(tài)詳解
這篇文章主要給大家介紹了關(guān)于c++中為何可以通過指針或引用實(shí)現(xiàn)多態(tài),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04

