日期:2014-05-16  浏览次数:20807 次

查看arp缓存出错
C/C++ code
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <net/if_arp.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

char *ip = "192.168.205.168";

int main()
{
    struct arpreq arp;
    /**/
    struct sockaddr_in * paddr = (struct sockaddr_in *)&arp.arp_pa;;
    int s, ret = 0;
    char *mac = arp.arp_pa.sa_data;
    
    s = socket(AF_INET, SOCK_STREAM, 0);
    
    paddr->sin_family = AF_INET;
    paddr->sin_port = htons(9090);
    paddr->sin_addr.s_addr = inet_addr(ip);
    
    strcpy(arp.arp_dev, "eth0");
    
    ret = ioctl(s, SIOCGARP, &arp);
    if(ret == -1)
         {
             perror("ioctl SIOCGARP");
             exit(1);
          }
    
    printf("%2x-%2x-%2x-%2x-%2x-%2x\n",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
    
    return 0;
}

编译没问题,运行出现ioctl SIOCGARP: Invalid argument错误,请问这是什么原因,该怎么改?

------解决方案--------------------
开发不会
不过看错误应该是参数错误 是不是缺头文件?
------解决方案--------------------
socket有没有成功返回?