Getting an IP address and port number from a sockaddr_in struct in Swift?

允我心安 提交于 2019-12-10 11:40:50

问题


After much trial and error, and without any success, it seems I might need a bit of help on this one:

How can I get IP address and port number from a sockaddr_in in the latest version of Swift?

I saw some related questions but can't seem to find a proper example anywhere. Also, I don't really seem to grasp how C type structs and pointers need to be handled in Swift, which doesn't really help.

Can anyone please supply me with an example or link to useful resources?

Many thanks in advance!


回答1:


If you need the IP address and port as numbers then you can access the corresponding fields of a sockaddr_in directly, but remember to convert the values from network byte (big endian) to host byte order:

let saddr: sockAddr = ...

let port = in_port_t(bigEndian: sockAddr.sin_port)
let addr = in_addr_t(bigEndian: sockAddr.sin_addr.s_addr)

getnameinfo() can be used to extract the IP address as a string (in dotted-decimal notation), and optionally the port as well. Casting a struct sockaddr_in pointer to a struct sockaddr pointer is called "rebinding" in Swift, and done with withMemoryRebound():

var sockAddr: sockaddr_in = ...

var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
var service = [CChar](repeating: 0, count: Int(NI_MAXSERV))

withUnsafePointer(to: &sockAddr) {
    $0.withMemoryRebound(to: sockaddr.self, capacity: 0) {
        _ = getnameinfo($0, socklen_t($0.pointee.sa_len),
                        &hostname, socklen_t(hostname.count),
                        &service, socklen_t(service.count),
                        NI_NUMERICHOST | NI_NUMERICSERV)
    }
}
print("addr:", hostname)
print("port:", service)

This works for both IPv4 and IPv6 socket address structures (sockaddr_in and sockaddr_in6).

For more information about "unsafe pointer conversions", see SE-0107 UnsafeRawPointer API and UnsafeRawPointer Migration. The latter page contains example code how to handle socket addresses in Swift 3.



来源:https://stackoverflow.com/questions/41494466/getting-an-ip-address-and-port-number-from-a-sockaddr-in-struct-in-swift

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