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

简介: 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 等地址转换函数的使用,能够帮助我们更加高效地处理网络通信和地址解析等任务。


目录
相关文章
|
9月前
|
安全 Linux 网络安全
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
253 0
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
|
10月前
|
运维 Linux 开发者
Linux系统中使用Python的ping3库进行网络连通性测试
以上步骤展示了如何利用 Python 的 `ping3` 库来检测网络连通性,并且提供了基本错误处理方法以确保程序能够优雅地处理各种意外情形。通过简洁明快、易读易懂、实操性强等特点使得该方法非常适合开发者或系统管理员快速集成至自动化工具链之内进行日常运维任务之需求满足。
675 18
|
10月前
|
网络协议 关系型数据库 Linux
【App Service Linux】在Linux App Service中安装 tcpdump 并抓取网络包
在App Service for Linux环境中,无法像Windows一样直接使用网络排查工具抓包。本文介绍了如何通过TCPDUMP在Linux环境下抓取网络包,包括SSH进入容器、安装tcpdump、执行抓包命令及下载分析文件的完整操作步骤。
496 5
|
11月前
|
Web App开发 网络协议 Linux
【Linux】网络基础
TCP/IP五层模型是网络通信的基础框架,将复杂的数据传输过程分为物理层、数据链路层、网络层、传输层和应用层,每层各司其职,协同完成远程通信。该模型确保了不同设备和网络之间的互联互通,是现代互联网运行的核心机制。
1299 5
|
11月前
|
网络协议 Linux 开发者
深入Linux中UDP网络通信机制编程探索
以上步骤概述了Linux中UDP网络通信的编程机制。在实现时,因关注细节和上下文环境可能有所调整,但大致流程是一致的。这些知识片段旨在帮助开发者快速上手Linux下的UDP编程,并提供可靠的信息作为编程的基础。在编程实践中,应结合实际业务需求,设计合适的数据传输协议,确保数据的正确性和实时性。
246 0
|
存储 Linux 编译器
Linux C/C++ 编程 内存管理之道:探寻编程世界中的思维乐趣
Linux C/C++ 编程 内存管理之道:探寻编程世界中的思维乐趣
441 0
|
关系型数据库 MySQL 数据库
Linux C/C++ 开发(学习笔记七):Mysql数据库C/C++编程实现 插入/读取/删除
Linux C/C++ 开发(学习笔记七):Mysql数据库C/C++编程实现 插入/读取/删除
688 0
|
Shell Linux C++
Linux C/C++ 开发(学习笔记二):Shell脚本编程案例
Linux C/C++ 开发(学习笔记二):Shell脚本编程案例
370 0
|
Linux C语言 C++
Linux ICMP协议实现:C/C++编程指南
ICMP(Internet Control Message Protocol)是网络通信中的重要协议,用于在IP网络中传递错误消息和诊断信息。在Linux系统中,我们可以使用C/C++编程语言来实现基本的ICMP功能,例如发送ICMP回显请求(Ping)和解析ICMP消息。本文将带您深入了解C/C++实现的ICMP协议,包括套接字编程、构造ICMP报文、发送和接收ICMP消息等,以及提供实际的代码示例。
1848 1
|
算法 Linux Shell
Linux C/C++ 开发(学习笔记三):Linux C编程案例
Linux C/C++ 开发(学习笔记三):Linux C编程案例
159 0

热门文章

最新文章