C/C++ 获取自身IP与域名片段的示例代码

时间:2022-06-01 22:04:27

判断大端序小端序:

通常情况下,数值在内存中存储的方式有两种,一种是大尾字节序,另一种是小尾,比如0x01020304这样一个数值,如果用大尾方式存储,其存储方式为01 02 03 04而用小尾方式存储则是04 03 02 01,一般Windows操作系统兼容的CPU为小尾方式,而UNIX操作系统所兼容的CPU多为大尾方式,通过使用两种方法即可判断大小纬。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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("小端字节序 \n");
    else
        printf("大端字节序 \n");
}
 
// 直接转换法判断
void GetB()
{
    DWORD dwSmallNum = 0x01020304;
    if (dwSmallNum == htonl(dwSmallNum))
        printf("大端字节序 \n");
    else
        printf("小端字节序 \n");
}
 
int main(int argc, char *argv[])
{
    GetA();
    GetB();
 
    system("pause");
    return 0;
}

利用域名获取IP:

通过使用winsocket库中的gethostbyname()可以将一个域名所对应的别名,地址类型等信息提取出来。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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;
}

取自身主机名IP地址:

有时候我们需要得到自身IP地址,这里我封装了两种获取IP地址的方法。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#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("本机名字: %s \n", hostname);
 
    char *hostaddr = GetLocalHostAddr(0);
    printf("本机IP: %s \n", hostaddr);
 
    system("pause");
    return 0;
}

以上就是C/C++ 获取自身IP与域名片段的示例代码的详细内容,更多关于C/C++ 获取自身IP与域名片段的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/LyShark/p/13655896.html