我正在尝试使用gethostbyname()
带有交叉编译器的函数来编译程序arm-none-linux-gnueabi
,但是当我在 android 上运行我的二进制文件时它不起作用。
我的代码在下面:
/* gethostbyname-example.c */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
extern int h_errno;
int main(int argc,char **argv) {
int x, x2;
struct hostent *hp;
for ( x=1; x<argc; ++x ) {
hp = gethostbyname(argv[x]);
if ( !hp ) {
fprintf(stderr,
"%s: host '%s'\n",
hstrerror(h_errno),
argv[x]);
continue;
}
printf("Host %s : \n" ,argv[x]);
printf(" Officially:\t%s\n", hp->h_name);
fputs(" Aliases:\t",stdout);
for ( x2=0; hp->h_aliases[x2]; ++x2 ) {
if ( x2 ) {
fputs(", ",stdout);
}
fputs(hp->h_aliases[x2],stdout);
}
fputc('\n',stdout);
printf(" Type:\t\t%s\n",
hp->h_addrtype == AF_INET
? "AF_INET" : "AF_INET6");
if ( hp->h_addrtype == AF_INET ) {
for ( x2=0; hp->h_addr_list[x2]; ++x2 ) {
printf(" Address:\t%s\n",
inet_ntoa( *(struct in_addr *)
hp->h_addr_list[x2]));
}
}
putchar('\n');
}
return 0;
}
我arm-none-linux-gnueabi-gcc
用操作系统ubuntu 12.04
和交叉编译器在我的笔记本电脑上编译Sourcery Codebench
:
$ arm-none-linux-gnueabi-gcc gethostbyname-example.c --static -o gethostbyname-example
/tmp/ccE0xjBG.o: In function `main':
lookup.c:(.text+0x38): warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
$ ls
gethostbyname-example.c gethostbyname-example
$ file gethostbyname-example
gethostbyname-example: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, for GNU/Linux 2.6.16, not stripped
然后我gethostbyname-example
在我的 chroot 手臂上测试二进制文件,我将二进制文件复制粘贴到chroot
文件夹,然后我登录到chroot
,然后我执行,顺便说一句,我从这里https://wiki.ubuntu.com/ARM/BuildEABIChroot创建 chroot ,如果我在 chroot 上运行它,得到如下结果:
# ./gethostbyname-example www.google.com
Host google.com :
Officially: www.google.com
Aliases:
Type: AF_INET
Address: 74.125.135.139
Address: 74.125.135.100
Address: 74.125.135.101
Address: 74.125.135.102
Address: 74.125.135.113
Address: 74.125.135.138
然后,我还将使用 adb push 的二进制 gethostbyname-example 上传到我的 android 设备,如果我运行二进制 gethostbyname-example,则会收到如下错误:
# ./gethostbyname-example www.google.com # Unknown server error: host 'www.google.com' # ping www.google.com # PING www.google.com (74.125.135.99) 56(84) bytes of data. # 64 bytes from ni-in-f99.1e100.net (74.125.135.99): icmp_seq=1 ttl=49 time=798 ms # 64 bytes from ni-in-f99.1e100.net (74.125.135.99): icmp_seq=2 ttl=49 time=1039 ms
在我的 android 设备中,我在 /etc/ 和 /system/etc/ 中有一个文件主机,带有谷歌 DNS
8.8.8.8
8.8.4.4
大概是什么原因导致这个失败,
谢谢你。