Ios Objective-C中为什么会出现错误

Ios Objective-C中为什么会出现错误,ios,objective-c,arp,Ios,Objective C,Arp,我是新手 我想通过UI设置打开热点,并通过objective-c获取连接到此热点的客户端的IP地址和MAC地址 我参考链接以了解更多信息 代码如下所示: #include <stdio.h> #include <sys/param.h> #include <sys/file.h> #include <sys/socket.h> #include <sys/sysctl.h> #include <net/if.h> #inc

我是新手

我想通过UI设置打开热点,并通过objective-c获取连接到此热点的客户端的IP地址和MAC地址

我参考链接以了解更多信息

代码如下所示:

#include <stdio.h>
#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 "if_types.h"
#include "route.h"
#include "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>

-(NSString*) ip2mac: (char*) ip
{



    int expire_time, flags, export_only, doing_proxy, found_entry;



    NSString *mAddr = nil;
    u_long addr = inet_addr(ip);
    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;

    mib[0] = CTL_NET;
    mib[1] = PF_ROUTE;
    mib[2] = 0;
    mib[3] = AF_INET;
    mib[4] = NET_RT_FLAGS;
    mib[5] = RTF_LLINFO;
    if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
        err(1, "route-sysctl-estimate");
        if ((buf = malloc(needed)) == NULL)
            err(1, "malloc");
            if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
                err(1, "actual retrieval of routing table");


                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);
                    if (addr) {
                        if (addr != sin->sin_addr.s_addr)
                            continue;
                        found_entry = 1;
                    }
                    if (nflag == 0)
                        hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
                                           sizeof sin->sin_addr, AF_INET);
                    else
                        hp = 0;
                    if (hp)
                        host = hp->h_name;
                    else {
                        host = "?";
                        if (h_errno == TRY_AGAIN)
                            nflag = 1;
                    }



                    if (sdl->sdl_alen) {

                        u_char *cp = LLADDR(sdl);

                        mAddr = [NSString stringWithFormat:@"%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]];


                        //  ether_print((u_char *)LLADDR(sdl));
                    }
                    else

                        mAddr = nil;

                }

    if (found_entry == 0) {
        return nil;
    } else {
        return mAddr;
    }
}
有人能教我如何使用上述代码从ARP表中获取IP地址吗?

假设您的方法-NSString*ip2mac:char*IP在一个类中 你的包裹搞糟了。如果无法更正,请先更正这些错误,然后替换以下内容以删除错误

#include <stdio.h>
#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 <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>
#include <net/if_types.h>
#include <net/route.h>
#include <netinet/if_ether.h>
.m文件

#import "MyClass.h"
#include <stdio.h>
#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 <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>
#include <net/if_types.h>
#include <net/route.h>
#include <netinet/if_ether.h>


@implementation MyClass {
  BOOL nflag;
}


-(NSString*) ip2mac: (char*) ip
{



  int expire_time, flags, export_only, doing_proxy, found_entry;



  NSString *mAddr = nil;
  u_long addr = inet_addr(ip);
  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;

  mib[0] = CTL_NET;
  mib[1] = PF_ROUTE;
  mib[2] = 0;
  mib[3] = AF_INET;
  mib[4] = NET_RT_FLAGS;
  mib[5] = RTF_LLINFO;
  if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    err(1, "route-sysctl-estimate");
  if ((buf = malloc(needed)) == NULL)
    err(1, "malloc");
  if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    err(1, "actual retrieval of routing table");


  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);
    if (addr) {
      if (addr != sin->sin_addr.s_addr)
        continue;
      found_entry = 1;
    }
    if (nflag == 0)
      hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
                         sizeof sin->sin_addr, AF_INET);
    else
      hp = 0;
    if (hp)
      host = hp->h_name;
    else {
      host = "?";
      if (h_errno == TRY_AGAIN)
        nflag = 1;
    }



    if (sdl->sdl_alen) {

      u_char *cp = LLADDR(sdl);

      mAddr = [NSString stringWithFormat:@"%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]];


      //  ether_print((u_char *)LLADDR(sdl));
    }
    else

      mAddr = nil;

  }

  if (found_entry == 0) {
    return nil;
  } else {
    return mAddr;
  }
}

@end

您正在使用目标C类进行C项目。您声明了一个方法,而不是一个C函数。您应该将代码迁移到C或Objective C。因此,函数是返回函数的类型名称_arguments@Martin,不能在类的作用域之外声明ObjC方法。Marting@Francesco是正确的。您应该将此方法移动到类中,或者将其设置为c函数,通过使用下面的导入,您可以删除这些错误。我对你问题的回答假设这个方法在一个类中对不起…我想用链接中的代码从ARP表中获取IP地址。你能教我如何使用上面的代码从ARP表中获取IP地址吗?我已经编辑了我想问的内容。对不起…我是新手,你的意思是-NSString*ip2mac:char*IP在一个类中?代码是我从链接中引用的。我想使用链接中的代码从ARP表中获取IP地址。您可以在原始帖子上发表评论以了解更多信息。您正在定义一个实例方法。实例方法可以在@接口中声明,并在@实现中定义。你真的应该得到一本解释Objective-C基础知识的好书。@inderkumarratore我已经编辑了我想问的内容。
#import "MyClass.h"
#include <stdio.h>
#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 <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>
#include <net/if_types.h>
#include <net/route.h>
#include <netinet/if_ether.h>


@implementation MyClass {
  BOOL nflag;
}


-(NSString*) ip2mac: (char*) ip
{



  int expire_time, flags, export_only, doing_proxy, found_entry;



  NSString *mAddr = nil;
  u_long addr = inet_addr(ip);
  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;

  mib[0] = CTL_NET;
  mib[1] = PF_ROUTE;
  mib[2] = 0;
  mib[3] = AF_INET;
  mib[4] = NET_RT_FLAGS;
  mib[5] = RTF_LLINFO;
  if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    err(1, "route-sysctl-estimate");
  if ((buf = malloc(needed)) == NULL)
    err(1, "malloc");
  if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    err(1, "actual retrieval of routing table");


  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);
    if (addr) {
      if (addr != sin->sin_addr.s_addr)
        continue;
      found_entry = 1;
    }
    if (nflag == 0)
      hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
                         sizeof sin->sin_addr, AF_INET);
    else
      hp = 0;
    if (hp)
      host = hp->h_name;
    else {
      host = "?";
      if (h_errno == TRY_AGAIN)
        nflag = 1;
    }



    if (sdl->sdl_alen) {

      u_char *cp = LLADDR(sdl);

      mAddr = [NSString stringWithFormat:@"%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]];


      //  ether_print((u_char *)LLADDR(sdl));
    }
    else

      mAddr = nil;

  }

  if (found_entry == 0) {
    return nil;
  } else {
    return mAddr;
  }
}

@end