当前位置: 首页 > news >正文

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

http://www.lryc.cn/news/64800.html

相关文章:

  • 庖丁解牛函数知识---C语言《2》
  • Git 使用教程:最详细、最正宗手把手教学(万字长文)
  • 【华为OD机试 2023最新 】最优资源分配/芯片资源占用(C语言题解 100%)
  • markdown二元运算符
  • 【华为/华三】PPP
  • 【Java笔试强训 9】
  • 【C++】STL标准库之list
  • Nomogram | 盘点一下绘制列线图的几个R包!~(二)
  • Django之定时任务django-crontab
  • linux常见命令
  • 【14.HTML-移动端适配】
  • 平衡二叉树旋转机制
  • 深入浅出C++ ——C++11
  • 智能座舱3.0阶段,看全球巨头如何打造更具“价值”的第三空间
  • 【Linux】入门介绍
  • 【Python】序列类型②-元组
  • 循环的数字
  • MySQL查询之聚合函数查询
  • 普通2本,去过字节外包,到现在年薪25W+的测试开发,我的2年转行心酸经历...
  • util.callbackify
  • 解决使用CLIP模型时TypeError: Cannot handle this data type: (1, 1, 224, 224), |u1
  • Mysql第二章 多表查询的操作
  • ESP32-CAM:TinyML 图像分类——水果与蔬菜
  • 如何防止订单重复支付
  • 不是那么快乐的五一
  • Maven命令和配置详解
  • P3029 [USACO11NOV]Cow Lineup S 双指针 单调队列
  • 数据结构与算法之链表: Leetcode 83. 删除排序链表中的重复元素 (Typescript版)
  • ubuntu16.04升级到20.04后报错 By not providing “FindEigen.cmake“
  • 设计模式——模板方法模式