iOS 上的 UDP 广播/设备发现? [英] UDP Broadcast/Device Discovery on iOS?

查看:32
本文介绍了iOS 上的 UDP 广播/设备发现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一种从 iOS 应用程序中发现本地网络上的 Logitech Harmony Hub 设备的方法.这个概念的灵感来自 这个 NODE.JS 项目,它似乎向 255.255.255.255 地址发送 UDP 广播,然后获取 Logitech 的 IP 地址(这就是我所追求的).从我的 Mac 在我的家庭网络上测试 NODE.JS 项目时,它成功找到了 Logitech Harmony Hub.

I am working on trying develop a means of discovering Logitech Harmony Hub devices on my local network, from an iOS app. 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). When testing the NODE.JS project on my home network from my Mac, it successfully finds the Logitech Harmony Hub.

我正在使用 CocoaASyncSocket,并且必须承认,我对 UDP 广播/发现如何工作的理解在这里可能是歪曲的.这就是我正在做的事情;

I am using CocoaASyncSocket, and must admit, my understanding of how UDP broadcast/discovery works may be askew here. 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.

谢谢!

推荐答案

从代码看来,您似乎只实现了解决方案的一半.它的工作方式是:

From the looks of the code it seems that you have only implemented half of the solution. The way it works is:

  • 向端口 5224 发送广播消息.该消息包括字符串 logitech-reverse-bonjour._tcp.local. 加上 Harmony 应连接回的端口号 - 在您的情况下已硬编码 61991.
  • 大概 Harmony 收到此数据包,识别该消息,然后启动与在指定端口(在本例中为 61991)上发送广播的设备的连接.
  • A broadcast message is sent to port 5224. This message includes the string logitech-reverse-bonjour._tcp.local. plus the port number that the Harmony should connect back to - in your case you have hardcoded 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).

由于您的应用未在此端口上侦听,因此您不会收到任何响应.这是在 responseCollector.js 文件中实现的node.js 项目

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

这篇关于iOS 上的 UDP 广播/设备发现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆