作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这段代码可以在本地网络上查找一些服务。如果我在我的 iPad 上安装该应用程序或使用模拟器它会工作正常,但如果我使用 TestFlight 我找不到任何服务。感谢您的帮助
import Foundation
class FindServices:NSObject,NetServiceDelegate,NetServiceBrowserDelegate {
let browser = NetServiceBrowser()
var arrayService = [NetService]()
var arrayHosts = [HostStructure]()
init(netService: String, domain: String) {
super.init()
browser.delegate = self
browser.searchForServices(ofType: netService, inDomain: domain)
//var arrayHosts = hostStuct.arrayHosts
}
func stopService(){
browser.stop()
browser.delegate = nil
}
//MARK: Delegates
func netServiceBrowser(_ browser: NetServiceBrowser, didFind service: NetService, moreComing: Bool) {
service.delegate = self
service.resolve(withTimeout: 0.0)
arrayService.append(service)
}
func netServiceBrowser(_ browser: NetServiceBrowser, didRemove service: NetService, moreComing: Bool) {
}
func netServiceBrowserWillSearch(_ browser: NetServiceBrowser) {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "FindInstancesWillSearch"), object: nil)
}
func netServiceBrowser(_ browser: NetServiceBrowser, didNotSearch errorDict: [String : NSNumber]) {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "FindInstancesStopSearch"), object: nil)
}
func netServiceDidResolveAddress(_ sender: NetService) {
if let addresses = sender.addresses, addresses.count > 0 {
for address in addresses {
let data = address as NSData
let inetAddress: sockaddr_in = data.castToCPointer()
if inetAddress.sin_family == __uint8_t(AF_INET) {
if let ip = String(cString: inet_ntoa(inetAddress.sin_addr), encoding: .ascii) {
// IPv4
print(ip)
let port = String(UInt16(inetAddress.sin_port).byteSwapped)
addToArrayHost(name: sender.name, ip: ip, port: port)
}
} else if inetAddress.sin_family == __uint8_t(AF_INET6) {
let inetAddress6: sockaddr_in6 = data.castToCPointer()
let ipStringBuffer = UnsafeMutablePointer<Int8>.allocate(capacity: Int(INET6_ADDRSTRLEN))
var addr = inetAddress6.sin6_addr
if let ipString = inet_ntop(Int32(inetAddress6.sin6_family), &addr, ipStringBuffer, __uint32_t(INET6_ADDRSTRLEN)) {
if let ip = String(cString: ipString, encoding: .ascii) {
// IPv6
print(ip)
}
}
ipStringBuffer.deallocate(capacity: Int(INET6_ADDRSTRLEN))
}
}
}
}
func netService(_ sender: NetService, didNotResolve errorDict: [String : NSNumber]) {
}
func getAddress(data: NSData){
}
//MARK ARRAY CONTROL AND NOTIFICATION
func addToArrayHost(name: String, ip: String, port: String){
arrayHosts.append(HostStructure(name: name, ip: ip, port: port, username: "", password: ""))
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "FindInstancesNewService"), object: nil)
}
func getHosts ()->[HostStructure]{
return arrayHosts
}
}
extension NSData {
func castToCPointer<T>() -> T {
let mem = UnsafeMutablePointer<T>.allocate(capacity: MemoryLayout<T.Type>.size)
self.getBytes(mem, length: MemoryLayout<T.Type>.size)
return mem.move()
}
}
最佳答案
尝试使用此代码获取地址:
for address in sender.addresses {
do {
var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
try address.withUnsafeBytes { (pointer:UnsafePointer<sockaddr>) -> Void in
guard getnameinfo(pointer, socklen_t(address.count), &hostname, socklen_t(hostname.count), nil, 0, NI_NUMERICHOST) == 0 else {
throw NSError(domain: "domain", code: 0, userInfo: ["error":"unable to get ip address"])
}
}
let ipAddress = String(cString: hostname)
// heres your IP!
} catch {
print(error)
}
}
return parsedAddresses
关于ios - Swift - NetServiceBrowser 在 TestFlight 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41533932/
我现在正在编写一个Bonjour服务监听器类,根据文档here: 目前,它似乎有效,我可以正确接收“netServiceBrowserWillSearch:”和“didFindService:more
我希望做这样的事情: 在 netServiceBrowser:didFindService:moreComing: [self.foundServices addObject:aNetService]
最近我将手机更新到 iOS 14,此后 netServiceBrowser.searchForServices(ofType:"xyz._tcp", inDomain: "local.") 根本无法工
我正在尝试运行 NetServiceBrowser。一旦新的 ZeroConf 设备出现在网络上,它将立即检测到该设备。但是,它不会检测设备何时从网络中删除。 netServiceBrowser(aN
我有这段代码可以在本地网络上查找一些服务。如果我在我的 iPad 上安装该应用程序或使用模拟器它会工作正常,但如果我使用 TestFlight 我找不到任何服务。感谢您的帮助 import Found
我是一名优秀的程序员,十分优秀!