我在linux中编写了以下代码(ubuntu 12.04 linux kernel 3.12.17),并且它比较成功,但是当我用eclipse移动android中的代码,并与ndk进行比较时,它显示错误:分配时不兼容的类型输入' struct in_addr'来自类型' int'。
addrs = inet_makeaddr(inet_netof(sin->sin_addr), INADDR_ANY);
整个代码是:
#include <jni.h>
#include <linux/if.h>
#include <linux/in.h>
#include <linux/sockios.h>
#include <stdint.h>
#include <sys/endian.h>
static struct in_addr *getbroadcastaddr(char *inter, int sock, char *buf){
struct ifconf ifc;
struct ifreq ifreq,*ifr;
static struct in_addr addrs;
struct sockaddr_in *sin;
bzero(&addrs,sizeof(addrs));
ifc.ifc_len = UDPMSGSIZE;
ifc.ifc_buf = buf;
if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
perror("broadcast: ioctl (get interface configuration)");
return(&addrs);
}
ifr = ifc.ifc_req;
for (ifr=ifc.ifc_req;ifr->ifr_name[0];ifr++) {
if (strcmp(ifr->ifr_name,inter)) continue;
ifreq = *ifr;
if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
perror("broadcast: ioctl (get interface flags)");
continue;
}
if ((ifreq.ifr_flags & IFF_BROADCAST) &&
(ifreq.ifr_flags & IFF_UP) &&
(ifr->ifr_addr.sa_family == AF_INET)) {
sin = (struct sockaddr_in *)&ifr->ifr_addr;
if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
addrs = inet_makeaddr(inet_netof(sin->sin_addr), INADDR_ANY);
} else {
addrs = ((struct sockaddr_in *)
(&ifreq.ifr_broadaddr))->sin_addr;
}
printf("Interface : %s\n",ifr->ifr_name);
return(&addrs);
}
}
return(NULL);
}
为什么呢?做了功能&#39; inet_makeaddr&#39;在android-ndk中是不同的枪?
答案 0 :(得分:1)
错误消息具有误导性,早期版本的NDK中缺少该功能。编译器产生不兼容的类型错误,因为当函数未定义时,它假定返回类型为int
。
相关文件是(NDK文件夹)/platforms/android-xx/arch-yyy/usr/include/arpa/inet.h
您可以通过更新为the latest version of the NDK来解决您的问题。自inet_makeaddr
inet.h
定义了android-21
如果之后仍然出现编译错误,请尝试
#include <arpa/inet.h>