GCDAsyncUdpSocket immediately closes when sending to an IPv6 address

前端 未结 2 1549
走了就别回头了
走了就别回头了 2021-01-14 11:23

I\'m connecting via UDP to a server on a different device which is advertised by Bonjour. When both the iOS device which this code is running on, and the server, are on our

2条回答
  •  醉话见心
    2021-01-14 12:16

    fe80 is a link-local IPv6 address. The machine to which you're connecting must have more than one network interface -- most do, e.g. Ethernet and WiFi. To fully specific an IPv6 address, the scope_id is required. This is the sin6_scope_id from:

    // IPv6 AF_INET6 sockets:
    
    struct sockaddr_in6 {
        u_int16_t       sin6_family;   // address family, AF_INET6
        u_int16_t       sin6_port;     // port number, Network Byte Order
        u_int32_t       sin6_flowinfo; // IPv6 flow information
        struct in6_addr sin6_addr;     // IPv6 address
        u_int32_t       sin6_scope_id; // Scope ID
    };
    

    and when combined with the address and converted to a string looks like this: fe80::e2f8:47ff:fe23:5392%eth1

    When the DNS is resolved, the NSData wrapping a sockaddr struct includes this information. However, in your code, you are extracting the sin6_port and sin6_addr, then feeding them back to GCDAsyncUDPSocket devoid of the sin6_flowinfo (which you don't need) and the sin6_scope_id (which in this case you do).

    Use -[GCDAsyncUDPSocket connectToAddress:error:] directly, using the NSData you get directly from your resolve service, and you should be good to go.

提交回复
热议问题