简体   繁体   English

iOS上的UDP广播/设备发现?

[英]UDP Broadcast/Device Discovery on iOS?

I am working on trying develop a means of discovering Logitech Harmony Hub devices on my local network, from an iOS app. 我正在努力尝试开发一种从iOS应用程序在我的本地网络上发现Logitech Harmony Hub设备的方法。 The concept is inspired by this NODE.JS project, which seems to send out a UDP broadcast to the 255.255.255.255 address, and then procures the Logitech's IP address (which is all I'm after). 这个概念的灵感来自于这个 NODE.JS项目,该项目似乎发送了一个UDP广播到255.255.255.255地址,然后获得了罗技的IP地址(这就是我所追求的)。 When testing the NODE.JS project on my home network from my Mac, it successfully finds the Logitech Harmony Hub. 当我在Mac上测试家庭网络上的NODE.JS项目时,它成功找到了Logitech Harmony Hub。

I am using CocoaASyncSocket , and must admit, my understanding of how UDP broadcast/discovery works may be askew here. 我正在使用CocoaASyncSocket ,并且必须承认,我对UDP广播/发现如何工作的理解可能在这里是歪斜的。 Here's what I'm doing; 这就是我正在做的事情;

import UIKit
import CocoaAsyncSocket

class ViewController: UIViewController, GCDAsyncUdpSocketDelegate {

var address = "255.255.255.255"
var port:UInt16 = 5224
var socket:GCDAsyncUdpSocket!
var socketReceive:GCDAsyncUdpSocket!
var error : NSError?

override func viewDidLoad() {
    super.viewDidLoad()

    let message = "_logitech-reverse-bonjour._tcp.local.\n61991".dataUsingEncoding(NSUTF8StringEncoding)

    socket = GCDAsyncUdpSocket(delegate: self, delegateQueue: dispatch_get_main_queue())
    socket.sendData(message, toHost: address, port: port, withTimeout: 1000, tag: 0)

    do {
        try socket.bindToPort(port)
    } catch {
        print(error)
    }

    do {
        try socket.enableBroadcast(true)
    } catch {
        print(error)
    }

    do {
        try socket.beginReceiving()
    } catch {
        print(error)
    }

}

    func udpSocket(sock: GCDAsyncUdpSocket!, didConnectToAddress address: NSData!) {
        print("didConnectToAddress");
    }

    func udpSocket(sock: GCDAsyncUdpSocket!, didNotConnect error: NSError!) {
        print("didNotConnect \(error)")
    }

    func udpSocket(sock: GCDAsyncUdpSocket!, didSendDataWithTag tag: Int) {
        print("didSendDataWithTag")
    } 

    func udpSocket(sock: GCDAsyncUdpSocket!, didNotSendDataWithTag tag: Int, dueToError error: NSError!) {
        print("didNotSendDataWithTag")
    }

func udpSocket(sock: GCDAsyncUdpSocket!, didReceiveData data: NSData!, fromAddress address: NSData!, withFilterContext filterContext: AnyObject!) {

    var host: NSString?
    var port1: UInt16 = 0
    GCDAsyncUdpSocket.getHost(&host, port: &port1, fromAddress: address)
    print("From \(host!)")

    let gotdata: NSString = NSString(data: data!, encoding: NSUTF8StringEncoding)!
    print(gotdata)

}

}

When I compile this, the only response I get is the message I just sent out; 当我编译它时,我得到的唯一响应是我刚发出的消息;

didSendDataWithTag
From ::ffff:192.168.1.101
_logitech-reverse-bonjour._tcp.local.
61991
From 192.168.1.101
_logitech-reverse-bonjour._tcp.local.
61991

I fear that I have a conceptual understanding issue with the broadcast here, and am sincerely hoping that someone may be able to point me to a resource or help to understand why I'm not getting any response from the device in my code. 我担心我在这里对广播有一个概念上的理解问题,我真诚地希望有人能够指出我的资源或帮助理解为什么我没有从我的代码中的设备得到任何响应。

Thanks! 谢谢!

From the looks of the code it seems that you have only implemented half of the solution. 从代码的外观来看,您似乎只实现了一半的解决方案。 The way it works is: 它的工作方式是:

  • A broadcast message is sent to port 5224. This message includes the string logitech-reverse-bonjour._tcp.local. 广播消息被发送到端口5224.该消息包括字符串logitech-reverse-bonjour._tcp.local. plus the port number that the Harmony should connect back to - in your case you have hardcoded 61991. 加上Harmony应该连接的端口号 - 在你的情况下你有硬编码的61991。
  • Presumably the Harmony receives this packet, recognises the message and then initiates a connection back to the device that sent the broadcast on the nominated port (61991 in this case). 据推测,Harmony收到此数据包,识别该消息,然后启动连接回到在指定端口上发送广播的设备(在本例中为61991)。

Since your app is not listening on this port you don't get any response. 由于您的应用未在此端口上侦听,因此您无法获得任何响应。 This is implemented in the responseCollector.js file in the node.js project 这是在node.js项目的responseCollector.js文件中实现的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM