您的位置:首页 > 移动开发 > IOS开发

iOS设备打印连接到同一Wifi的其余设备清单

2014-04-11 10:04 148 查看
打印Arp

1.首先引入头文件

#include <sys/param.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <sys/sysctl.h>

#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/route.h>
#include <netinet/if_ether.h>
#include <netinet/in.h>

#include <arpa/inet.h>

#include <err.h>
#include <errno.h>
#include <netdb.h>

#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


2.添加函数

- (void)printArp
{
int mib[6];
size_t needed;
char *host, *lim, *buf, *next;
struct rt_msghdr *rtm;
struct sockaddr_inarp *sin;
struct sockaddr_dl *sdl;
extern int h_errno;
struct hostent *hp = NULL;

mib[0] = CTL_NET;
mib[1] = PF_ROUTE;
mib[2] = 0;
mib[3] = AF_INET;
mib[4] = NET_RT_FLAGS;
mib[5] = RTF_LLINFO;

sysctl(mib, 6, NULL, &needed, NULL, 0);
buf = malloc(needed);

sysctl(mib, 6, buf, &needed, NULL, 0);
lim = buf + needed;

for (next = buf; next < lim; next += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *)next;
sin = (struct sockaddr_inarp *)(rtm + 1);
sdl = (struct sockaddr_dl *)(sin + 1);

hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
sizeof sin->sin_addr, AF_INET);
host = hp->h_name;

printf("%s--%s--at ", host, inet_ntoa(sin->sin_addr));
if (sdl->sdl_alen)
{
u_char *cp = (u_char *)LLADDR(sdl);
printf("%x:%x:%x:%x:%x:%x\n", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
}
}
}


3.调用上述函数即可

移动设备

Arp打印出来的只有PC,至于移动设备,我是用UDP广播识别的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: