Linux C/C++ 网络编程中地址格式转换(inet_pton和inet_ntop函数)

本文涉及的产品
公网NAT网关,每月750个小时 15CU
简介: Linux C/C++ 网络编程中地址格式转换(inet_pton和inet_ntop函数)

网络编程中地址格式转换(inet_pton和inet_ntop函数)

地址格式转换

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int  inet_pton(int  af ,  const  char  * src  ,void   *  dst);

(1)功能:将点分十进制格式的地址字符串转换为网络字节序整型数

(2)返回值:成功返回1,错误返回-1

(3)参数:

----af   :转换格式 AF_INET(IPV4)或者AF_INET6(IPV6)
    ---- src :点分格式的地址
     ----- dst:转换后的整型变量的地址
const char *  inet_ntop(int  af , const  void  * src ,  char * dst , socklen_t   cnt);

(1)功能:将网络字节序整型数转换为点分格式的IP地址

(2)返回值:成功返回转换后的地址,错误返回NULL

(3)参数:

----af   :转换格式 AF_INET(IPV4)或者AF_INET6(IPV6)
    ---- src :整型变量的地址
     ----- dst:用来存储转换后的数据的地址
    ------cnt:存储空间的大小

错误处理

在使用 inet_ptoninet_ntop 函数时,可能会出现一些错误。为了确保程序的稳定性,我们需要对这些错误进行适当的处理。通常情况下,我们可以使用 perror 函数来打印出具体的错误信息。下面我们将详细介绍如何处理这两个函数的错误,并给出一个错误处理的例子。

首先,我们需要注意 inet_pton 函数的返回值。当成功将地址字符串转换为网络字节序整数时,它会返回 1;如果输入的地址字符串无效,它会返回 0;发生错误时,它会返回 -1。因此,在调用 inet_pton 后,我们需要检查返回值,以确定是否发生了错误。如果返回值为 -1,我们可以使用 perror 函数打印出具体的错误信息。

同样地,对于 inet_ntop 函数,当成功将网络字节序整数转换为点分格式的 IP 地址时,它会返回转换后的地址;如果发生错误,它会返回 NULL。因此,在调用 inet_ntop 后,我们需要检查返回值,以确定是否发生了错误。如果返回值为 NULL,我们可以使用 perror 函数打印出具体的错误信息。

下面是一个处理错误的示例:

#include <stdio.h>
#include <stdlib.h>  
#include <sys/types.h>  
#include <sys/socket.h>  
#include <arpa/inet.h>  
#include <string.h>  
#include <netdb.h>  
int main()  
{  
    int addrnum;  
    char addrstring[] = "Invalid IP address";  
    printf("Address in dotted-quad format is %s\\n", addrstring);  
    if (inet_pton(AF_INET, addrstring, &addrnum) == -1) {
        perror("inet_pton");
        exit(EXIT_FAILURE);
    }
    char addrstring2[16] = "";
    if (inet_ntop(AF_INET, &addrnum, addrstring2, 16) == NULL) {
        perror("inet_ntop");
        exit(EXIT_FAILURE);
    }
    printf("Address in dotted-quad format is %s\\n", addrstring2);  
    return 0;  
}

运行结果:

  1. Address in dotted-quad format is Invalid IP address
  2. inet_pton: Invalid argument

在上面的示例中,我们使用了一个无效的 IP 地址字符串。当调用 inet_pton 函数时,它会返回 -1,表示发生了错误。我们使用 perror 函数打印出错误信息 “Invalid argument”,这表明输入的地址字符串无效。通过这种方式,我们可以轻松地处理 inet_ptoninet_ntop 函数的错误,并确保程序的稳定性。

示例

IPV4

#include <stdio.h>
#include <stdlib.h>  
#include <sys/types.h>  
#include <sys/socket.h>  
#include <arpa/inet.h>  
#include <string.h>  
#include <netdb.h>  
int main()  
{  
  int addrnum;  
    char addrstring[16] = "127.0.0.1";  
    printf("address in dotted-quad format is %s\\n",addrstring);  
    inet_pton(AF_INET,addrstring,&addrnum);  
    printf("address in network byteorder integer is 0x%x\\n",addrnum);  
    char addrstring2[16] = "";  
    if(inet_ntop(AF_INET,&addrnum,addrstring2,16) == NULL)  
      {  
               perror("inet_ntop");  
       }  
      printf("address in dotted - quad format is %s \\n",addrstring2);  
        return 0;  
 }     

运行结果:

[root@rac2 ~]# ./addrformat

  1. address in dotted-quad format is 127.0.0.1
  2. address in network byteorder integer is 0x100007f
  3. address in dotted - quad format is 127.0.0.1

IPV6

#include <stdio.h>
#include <stdlib.h>  
#include <sys/types.h>  
#include <sys/socket.h>  
#include <arpa/inet.h>  
#include <string.h>  
#include <netdb.h>  
int main()  
{  
    int addrnum;  
    char addrstring[INET6_ADDRSTRLEN] = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";  
    printf("IPv6 address in hexadecimal notation is %s\\n", addrstring);  
    struct in6_addr addrnum6;
    if (inet_pton(AF_INET6, addrstring, &addrnum6) != 1) {
        perror("inet_pton");
        exit(EXIT_FAILURE);
    }
    char addrstring2[INET6_ADDRSTRLEN] = "";
    if (inet_ntop(AF_INET6, &addrnum6, addrstring2, INET6_ADDRSTRLEN) == NULL) {
        perror("inet_ntop");
        exit(EXIT_FAILURE);
    }
    printf("IPv6 address in hexadecimal notation is %s\\n", addrstring2);  
    return 0;  
}

在C++ 中的使用示例

在 C++ 中,我们可以使用 RAII(Resource Acquisition Is Initialization)技术和异常处理机制来优雅地使用 inet_ptoninet_ntop 函数。下面是一个综合示例,包含注释和异常捕获:

#include <iostream>
#include <stdexcept>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <cstring>
class IPAddress
{
public:
    IPAddress(const std::string& address, int family)
        : family_(family)
    {
        if (family == AF_INET) {
            if (inet_pton(AF_INET, address.c_str(), &addr_.ipv4) != 1) {
                throw std::runtime_error("Invalid IPv4 address: " + address);
            }
        }
        else if (family == AF_INET6) {
            if (inet_pton(AF_INET6, address.c_str(), &addr_.ipv6) != 1) {
                throw std::runtime_error("Invalid IPv6 address: " + address);
            }
        }
        else {
            throw std::runtime_error("Invalid address family");
        }
    }
    std::string toString() const
    {
        char buffer[INET6_ADDRSTRLEN];
        const char* result = inet_ntop(family_, &addr_, buffer, sizeof(buffer));
        if (result == nullptr) {
            throw std::runtime_error("Address conversion failed");
        }
        return std::string(buffer);
    }
private:
    union {
        struct in_addr ipv4;
        struct in6_addr ipv6;
    } addr_;
    int family_;
};
int main()
{
    try {
        // IPv4 示例
        IPAddress ipv4("192.168.1.1", AF_INET);
        std::cout << "IPv4 address: " << ipv4.toString() << std::endl;
        // IPv6 示例
        IPAddress ipv6("2001:0db8:85a3:0000:0000:8a2e:0370:7334", AF_INET6);
        std::cout << "IPv6 address: " << ipv6.toString() << std::endl;
    }
    catch (const std::runtime_error& e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }
    return 0;
}

应用场景

地址转换在网络编程中具有广泛的应用场景。它的主要作用是在不同表示格式之间进行转换,以便于在各种网络通信和地址解析任务中使用。下面我们将简要介绍地址转换在实际应用场景中的一些用途。

  1. 网络通信:在网络编程中,我们需要与不同的网络设备进行通信。设备之间的通信通常使用网络字节序(大端字节序)表示的整数进行。而人们在日常使用中,通常使用点分十进制(IPv4)或者冒分十六进制(IPv6)表示的 IP 地址。因此,在实际编程过程中,我们需要将人类可读的地址转换为网络字节序整数,或者将网络字节序整数转换为人类可读的地址。这就需要使用 inet_ptoninet_ntop 等地址转换函数。
  2. 地址解析:在许多网络应用中,我们需要根据域名来获取 IP 地址。这个过程通常涉及到 DNS 查询,返回的 IP 地址通常以网络字节序整数的形式表示。为了方便展示和使用,我们需要将这些整数地址转换为点分十进制或冒分十六进制表示的字符串。这时,inet_ntop 函数就派上了用场。
  3. 跨平台兼容性:不同的计算机系统和网络设备可能使用不同的字节序。为了确保跨平台的兼容性,网络通信通常采用网络字节序(大端字节序)表示整数。地址转换函数如 inet_ptoninet_ntop 能够确保在不同平台之间进行正确的转换,从而提高了跨平台应用的兼容性。
  4. 网络诊断与调试:在网络诊断和调试过程中,我们需要处理各种地址信息。地址转换函数可以帮助我们将不同格式的地址信息转换为可读的形式,便于分析和排查问题。

通过以上介绍,我们可以看到地址转换在实际网络编程场景中具有重要的作用。掌握 inet_ptoninet_ntop 等地址转换函数的使用,能够帮助我们更加高效地处理网络通信和地址解析等任务。


目录
相关文章
|
7天前
|
机器学习/深度学习 缓存 监控
linux查看CPU、内存、网络、磁盘IO命令
`Linux`系统中,使用`top`命令查看CPU状态,要查看CPU详细信息,可利用`cat /proc/cpuinfo`相关命令。`free`命令用于查看内存使用情况。网络相关命令包括`ifconfig`(查看网卡状态)、`ifdown/ifup`(禁用/启用网卡)、`netstat`(列出网络连接,如`-tuln`组合)以及`nslookup`、`ping`、`telnet`、`traceroute`等。磁盘IO方面,`iostat`(如`-k -p ALL`)显示磁盘IO统计,`iotop`(如`-o -d 1`)则用于查看磁盘IO瓶颈。
|
4天前
|
网络协议 Linux Shell
【linux网络(一)】初识网络, 理解四层网络模型
【linux网络(一)】初识网络, 理解四层网络模型
|
5天前
|
安全 Ubuntu Linux
Linux 网络操作命令Telnet
Linux 网络操作命令Telnet
20 0
Linux 网络操作命令Telnet
|
5天前
|
Ubuntu Linux
Linux(22) Linux设置网络优先级顺序
Linux(22) Linux设置网络优先级顺序
6 0
|
6天前
|
Ubuntu 网络协议 Linux
Linux(20) Ubuntu 20.04 网络接口自动切换路由配置
Linux(20) Ubuntu 20.04 网络接口自动切换路由配置
28 0
|
17天前
|
NoSQL Linux Shell
常用的 Linux 命令
常用的 Linux 命令
36 9
|
1天前
|
Linux
【Linux】深入理解ls命令
【Linux】深入理解ls命令
|
4天前
|
监控 Linux Windows
50个必知的Linux命令技巧,你都掌握了吗?(下)
50个必知的Linux命令技巧,你都掌握了吗?(下)
|
4天前
|
Linux Shell Windows
Linux 常用基本命令
Linux 常用基本命令
|
5天前
|
Ubuntu Linux Shell
linux免交互登陆远程主机并执行命令(密钥对和Expect)
linux免交互登陆远程主机并执行命令(密钥对和Expect)