ios – 使用Swift中的CocoaAsyncSocket接收SSDP响应

ios – 使用Swift中的CocoaAsyncSocket接收SSDP响应,第1张

概述我正在尝试使用 swift与库 CocoaAsyncSocket( https://github.com/robbiehanson/CocoaAsyncSocket)接收SSDP响应 我可以成功地将M-Search命令发送到多播组并获得响应,我查看了wireshark(http://i.imgur.com/pn6LB7R.png):我可以看到M-Search数据包输出并且响应返回但我从未收到NSD 我正在尝试使用 swift与库 CocoaAsyncSocket( https://github.com/robbiehanson/CocoaAsyncSocket)接收SSDP响应

我可以成功地将M-Search命令发送到多播组并获得响应,我查看了wireshark(http://i.imgur.com/pn6LB7R.png):我可以看到M-Search数据包输出并且响应返回但我从未收到NSData在我的申请中. (顺便说一句,我现在正在寻找一个Roku)

我已经实现了GcdasyncUdpSocketDelegate委托并添加了dIDReceiveData函数,但是我从来没有得到过数据.

我怀疑这部分有问题:

ssdpSocket = GcdasyncUdpSocket(delegate: self,delegateQueue: dispatch_get_main_queue()) ssdpSocket.bindtoport(ssdpPort,error: &error)ssdpSocket.beginReceiving(&error)ssdpSocket.enablebroadcast(true,error: &error)ssdpSocket.connectToHost(ssdpAddres,onPort: ssdpPort,error: &error)ssdpSocket.joinMulticastGroup(ssdpAddres,error: &error)

请参阅以下示例代码:

import UIKitclass VIEwController: UIVIEwController,GcdasyncUdpSocketDelegate {//ssdp stuffvar ssdpAddres          = "239.255.255.250"var ssdpPort:UInt16     = 1900var ssdpSocket:GcdasyncUdpSocket!var ssdpSocketRec:GcdasyncUdpSocket!var error : NSError?overrIDe func vIEwDIDLoad() {    super.vIEwDIDLoad()    ssdpSocket = GcdasyncUdpSocket(delegate: self,delegateQueue: dispatch_get_main_queue())    ssdpSocket.bindtoport(ssdpPort,error: &error)    ssdpSocket.beginReceiving(&error)    ssdpSocket.enablebroadcast(true,error: &error)    ssdpSocket.connectToHost(ssdpAddres,error: &error)    ssdpSocket.joinMulticastGroup(ssdpAddres,error: &error)    //replace ST:roku:ecp with ST:ssdp:all to vIEw all devices    let data = "M-SEARCH * http/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\nMX: 3\r\nST: roku:ecp\r\nUSER-AGENT: iOS UPnP/1.1 TestApp/1.0\r\n\r\n".dataUsingEnCoding(NSUTF8StringEnCoding)    ssdpSocket.sendData(data,withTimeout: 1,tag: 0)}func udpSocket(sock:GcdasyncUdpSocket!,dIDConnectToAddress data : NSData!){    println("dIDConnectToAddress")    println(data)}func udpSocket(sock: GcdasyncUdpSocket!,dIDReceiveData data: NSData!,fromAddress address: NSData!,withFilterContext filterContext: AnyObject!) {    println("dIDReceiveData")    println(data)}overrIDe func dIDReceiveMemoryWarning() {    super.dIDReceiveMemoryWarning()    // dispose of any resources that can be recreated.}}
解决方法 在大量使用代码之后,我已经开始使用它了,我在这里发布代码以帮助其他人解决这个问题.

当我删除:

ssdpSocket.connectToHost(ssdpAddres,error: &error)

我能够接收数据,我怀疑这是以某种方式阻止它..不知道如何或为什么,我猜我不需要连接到主机作为其多播

它现在正在使用以下代码:

let mSearchData = "M-SEARCH * http/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\nMX: 3\r\nST: ssdp:all\r\nUSER-AGENT: iOS UPnP/1.1 TestApp/1.0\r\n\r\n".dataUsingEnCoding(NSUTF8StringEnCoding) //all devicesoverrIDe func vIEwDIDLoad() {    super.vIEwDIDLoad()    //send M-Search    ssdpSocket = GcdasyncUdpSocket(delegate: self,delegateQueue: dispatch_get_main_queue())    ssdpSocket.sendData(mSearchData,toHost: ssdpAddres,port: ssdpPort,tag: 0)    //bind for responses    ssdpSocket.bindtoport(ssdpPort,error: &error)    ssdpSocket.beginReceiving(&error)}

dIDReceiveData函数现在被调用,我可以打印响应:

func udpSocket(sock: GcdasyncUdpSocket!,withFilterContext filterContext: AnyObject!) {    var host: Nsstring?    var port1: UInt16 = 0    GcdasyncUdpSocket.getHost(&host,port: &port1,fromAddress: address)    println("From \(host!)")    let gotdata: Nsstring = Nsstring(data: data!,enCoding: NSUTF8StringEnCoding)!    println(gotdata) }
总结

以上是内存溢出为你收集整理的ios – 使用Swift中的CocoaAsyncSocket接收SSDP响应全部内容,希望文章能够帮你解决ios – 使用Swift中的CocoaAsyncSocket接收SSDP响应所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1094982.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存