红联Linux门户
Linux帮助

Error : Servname not supported for ai_socktype错误

发布时间:2017-02-18 11:24:43来源:blog.csdn.net/code_style作者:coding梦想_起点
针对在嵌入式arm Linux平台使用getaddrinfo,出现Error : Servname not supported for ai_socktype这个错误,网络上很多人的解释是需要在/etc/services作文章,但是嵌入式平台甚至精简到service这样的命令都没有,通过下面这段代码交叉编译之后,对使用不同的交叉编译工具进行测试,用arm-linux-gnueabi-gcc就会出现错误,用arm-linux-gcc就不会有这个错误,基本上判断是使用的glibc不同,实现不同。
 
笔者也是在编译ntpdate4.2.4P7 arm linux平台遇到的问题,使用arm-linux-gcc编译总是会出错,使用arm-linux-gnueabi-gcc可以编译,但是运行会报出如上错误。
 
测试1:
#arm-linux-gnueabi-gcc main.c -o main
 
测试2:
#arm-linux-gcc main.c -o main
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
int main()
{
int error=0;
char *serv="time.buptnet.edu.cn";
char *service="ntp";
struct addrinfo *addrResult;
/* Address infos structure to store hints for getaddrinfo */
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
error = getaddrinfo(serv, service, &hints, &addrResult);
if (error != 0) {
/* Conduct more refined error analysis */
if (error == EAI_FAIL || error == EAI_AGAIN){
/* Name server is unusable. Exit after failing on the
first server, in order to shorten the timeout caused
by waiting for resolution of several servers */
fprintf(stderr, "Name server cannot be used, exiting");
}
fprintf(stderr, "error=%d\n", error);
fprintf(stderr, "Error : %s\n", gai_strerror(error));
}
return 0;
}
 
本文永久更新地址:http://www.linuxdiyf.com/linux/28514.html