函式原型:
#include <arpa/inet.h>
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size); //"network to printable"
int inet_pton(int af, const char *src, void *dst);
一个使用例子:
char str[INET6_ADDRSTRLEN];
if(inet_ntop(AF_INET6, &sin6_addr, str, INET6_ADDRSTRLEN) == NULL){
perror("inet ntop/n");
printf("error\n");
}
syslog(LOG_ERR, "sin6_addr=%s\n", str);
printf("sin6_addr=%s\n", str);

推荐阅读:

Beej's Guide to Network Programming
English:
https://beej.us/guide/bgnet/html/multi/inet_ntopman.html
繁体中文,目录清晰:
http://beej-zhtw.netdpi.net/09-man-manual/9-14-inet_ntop-inet_pton

// IPv6 demo of inet_ntop() and inet_pton()
// (basically the same except with a bunch of 6s thrown around) struct sockaddr_in6 sa;
char str[INET6_ADDRSTRLEN]; // store this IP address in sa:
inet_pton(AF_INET6, "2001:db8:8714:3a90::12", &(sa.sin6_addr)); // now get it back and print it
inet_ntop(AF_INET6, &(sa.sin6_addr), str, INET6_ADDRSTRLEN); printf("%s\n", str); // prints "2001:db8:8714:3a90::12"
// Helper function you can use:

//Convert a struct sockaddr address to a string, IPv4 and IPv6:

char *get_ip_str(const struct sockaddr *sa, char *s, size_t maxlen)
{
switch(sa->sa_family) {
case AF_INET:
inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr),
s, maxlen);
break; case AF_INET6:
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr),
s, maxlen);
break; default:
strncpy(s, "Unknown AF", maxlen);
return NULL;
} return s;
}

参考:

网络编程中常见地址结构与转换(IPv4/IPv6)
https://www.cnblogs.com/sunada2005/archive/2013/08/06/3240724.html
Linux网络编程IPv4和IPv6的inet_addr、inet_aton、inet_pton等函数小结
https://blog.csdn.net/ithomer/article/details/6100734

一个例子很清晰:
https://gist.github.com/q2hide/244bf94d3b72cc17d9ca

最新文章

  1. mysql查询本周、月、季度、年
  2. php获取实时汇率数据
  3. DataInputStream和DataOutputStream
  4. iOS 8 Auto Layout界面自动布局系列2-使用Xcode的Interface Builder添加布局约束
  5. PHP性能分析 - ngnx日志分析
  6. Could not find the Visual SourceSafe Internet Web Service connection information for the specified database Would you like to launch the Visual sourceSafe connection wizard?
  7. python ImportError: No module named 的问题
  8. 导出数据库数据制成Excel和txt
  9. Maven中心仓库
  10. FileUpload上传文件无法获取文件名
  11. BZOJ_3252_攻略_线段树+dfs序
  12. using eclipse to write c programe 0
  13. (办公)SpringBoot和swagger2的整合.
  14. JMETER java.net.SocketException: Connection reset 报错解决方案
  15. python脚本执行报错整理
  16. 外卖ERP管理系统(一)
  17. ImportError: No module named &#39;requests.packages.urllib3&#39;
  18. L1-058 6翻了
  19. zabbix agentd安装
  20. java.lang.UnsatisfiedLinkError:no dll in java.library.path

热门文章

  1. Java Applet 基础
  2. CentOS6.8 安装FTP及添加用户
  3. JDBC技术总结(三)
  4. jira报错,此域不支持您输入的日期
  5. IBM Rational Appscan使用之扫描结果分析
  6. HTML5 2D平台游戏开发#5攻击
  7. Eclipse配置总结
  8. 改进Spring中的分页技术
  9. CentOS VSCode调试go语言出现:exec: &quot;gcc&quot;: executable file not found in PATH
  10. Android中应用安装分析