美文网首页
获取IP和端口号

获取IP和端口号

作者: Liu____ | 来源:发表于2024-01-08 14:39 被阅读0次
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <net/if.h>

#define IOS_CELLULAR    @"pdp_ip0"
#define IOS_WIFI        @"en0"
//#define IOS_VPN       @"utun0"
#define IP_ADDR_IPv4    @"ipv4"
#define IP_ADDR_IPv6    @"ipv6"

//获取所有相关IP信息
- (NSDictionary *)getIPAddresses
{
    NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];
    
    // retrieve the current interfaces - returns 0 on success
    struct ifaddrs *interfaces;
    if(!getifaddrs(&interfaces)) {
        // Loop through linked list of interfaces
        struct ifaddrs *interface;
        for(interface=interfaces; interface; interface=interface->ifa_next) {
            if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) {
                continue; // deeply nested code harder to read
            }
            const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr;
            char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];
            if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) {
                NSString *name = [NSString stringWithUTF8String:interface->ifa_name];
                NSString *type;
                if(addr->sin_family == AF_INET) {
                    if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) {
                        type = IP_ADDR_IPv4;
                        NSLog(@"对方PORT:%d ", ntohs(&addr->sin_port));
                    }
                } else {
                    const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr;
                    if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) {
                        type = IP_ADDR_IPv6;
                        NSLog(@"对方PORT:%d ", ntohs(&addr6->sin6_port));
                    }
                }
                if(type) {
                    NSString *key = [NSString stringWithFormat:@"%@/%@", name, type];
                    addresses[key] = [NSString stringWithUTF8String:addrBuf];
                }
            }
        }
        // Free memory
        freeifaddrs(interfaces);
    }
    return [addresses count] ? addresses : nil;
}

相关文章

  • 获取IP地址及端口号

    获取IP地址 获取端口号 或者 者或

  • ADB常用命令

    连接一台台同局域网下设备ip为192.168.0.123,端口号为5555的设备 获取设备列表及设备状态 获取设备...

  • java调用海康威视sdk注册失败

    根据官网的工具,获取该设备的ip,端口号(注意不是http端口),然后根据自己设定的用户名和密码,注册设备 返回错...

  • 2018-08-27 socket编程

    网络通信需要的东西:IP地址、端口号、协议 端口号:范围0-65535,0-1023为系统保留 IP地址+端口号=...

  • 修改django启动绑定的IP和端口

    修改django启动时绑定的ip和端口号

  • Docker镜像

    获取镜像命令格式 Docker Registry地址:地址的格式一般是 <域名/IP>[:端口号]。默认地址是 D...

  • 1 Java网络编程

    java网络编程 1 基础知识1)ip地址和端口号ip地址,一个通讯实体端口号,用于区分不同的通讯程序2)tcp/...

  • 详解TCP和UDP的区别?教你快速理解三次握手和四次挥手!

    前言: 之前我们有了解IP地址和端口号,通过IP地址能够找到对应的设备,然后再通过端口号找到对应的端口,再通过端口...

  • 镜像操作(四)

    一、获取镜像 Docker 镜像仓库地址:地址的格式一般是 <域名/IP>[:端口号]默认地址是 Docker H...

  • http headers之origin,host和referer

    Host - 请求头 指明了请求的服务器的域名/ip 地址和端口号 组成:域名+端口号 如果没有给定端口号,会自动...

网友评论

      本文标题:获取IP和端口号

      本文链接:https://www.haomeiwen.com/subject/nranndtx.html