GCDAsyncUDPSocket source address returns null

隐身守侯 提交于 2019-12-06 07:57:42

The address field return to you is actually a sockaddr_in structure flattened into an NSData object.

Here's the struct:

struct sockaddr_in {
    __uint8_t   sin_len;
    sa_family_t sin_family;
    in_port_t   sin_port;
    struct  in_addr sin_addr;
    char        sin_zero[8];
};

You can see from your display of the address object that the first field, sin_len, is 0x10, or 16 bytes. That's the length of the sockaddr_in struct. You can use that to tell if the address object refers to an IPv4 object or an IPv6 object. An IPv6 object would use the sockaddr_in6 struct and have a longer length.

You could copy that NSData object into a sockaddr_in struct, or just pull out the bytes at the right offset (4 through 7) to get the 4-byte source address that looks more familiar.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!