Linux socket编程中调用 inet_ntoa 函数产生的段错误 “Segmentation fault (core dumped)” 的原因及解决办法

调用 inet_ntoa 函数产生的段错误 “Segmentation fault (core dumped)” 的原因及解决办法

struct sockaddr_in   client_addr;
...
printf("connection from %s ", inet_ntoa(client_addr.sin_addr));

执行到 inet_ntoa 时会产生段错误:
Segmentation fault (core dumped)

【原因】
据说是在64位环境下会产生错误,而在32位系统下运行正常。

【解决办法】
加入头文件:
#include <arpa/inet.h>

编译后执行正常。

来源:https://blog.csdn.net/iw1210/article/details/11798245

原文地址:https://www.cnblogs.com/FengZeng666/p/12877984.html