zoukankan      html  css  js  c++  java
  • ios微信支付invalid spbill_create_ip 获取设备ip错误

    #import <sys/socket.h>

    #import <sys/sockio.h>

    #import <sys/ioctl.h>

    #import <net/if.h>

    #import <arpa/inet.h>

    +(NSString *)getDeviceIPAddresses {

        int sockfd = socket(AF_INET, SOCK_DGRAM, 0);

        NSMutableArray *ips = [NSMutableArray array];

        int BUFFERSIZE = 4096;

        struct ifconf ifc;

        char buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;

        struct ifreq *ifr, ifrcopy;

        ifc.ifc_len = BUFFERSIZE;

        ifc.ifc_buf = buffer;

        if (ioctl(sockfd, SIOCGIFCONF, &ifc) >= 0){

            for (ptr = buffer; ptr < buffer + ifc.ifc_len; ){

                ifr = (struct ifreq *)ptr;

                int len = sizeof(struct sockaddr);

                if (ifr->ifr_addr.sa_len > len) {

                    len = ifr->ifr_addr.sa_len;

                }

                

                ptr += sizeof(ifr->ifr_name) + len;

                if (ifr->ifr_addr.sa_family != AF_INET) continue;

                if ((cptr = (char *)strchr(ifr->ifr_name, ':')) != NULL) *cptr = 0;

                if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0) continue;

                

                memcpy(lastname, ifr->ifr_name, IFNAMSIZ);

                ifrcopy = *ifr;

                ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy);

                if ((ifrcopy.ifr_flags & IFF_UP) == 0) continue;

               NSString *ip = [NSString  stringWithFormat:@"%s", inet_ntoa(((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr)];

                [ips addObject:ip];

            }

        }

      close(sockfd);

        NSString *deviceIP = @"";

         for (int i=0; i < ips.count; i++) {

            if (ips.count > 0) {

                deviceIP = [NSString stringWithFormat:@"%@",ips.lastObject];

            }

        }

        return deviceIP;

    }

  • 相关阅读:
    python第二课
    python第一课
    Python基础20-类的创建与删增改查
    Python基础19模块-hashlib模块
    Python基础18模块-configerparse模块
    Python基础17模块-logging模块
    Python基础16模块-re模块
    Python基础-15模块-OS模块
    Python基础-14模块-random模块
    Python基础-13模块-时间模块
  • 原文地址:https://www.cnblogs.com/akiha/p/6842113.html
Copyright © 2011-2022 走看看