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

前端之家收集整理的这篇文章主要介绍了ios – 使用Swift中的CocoaAsyncSocket接收SSDP响应前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用 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 UIKit
class ViewController: UIViewController,GCDAsyncUdpSocketDelegate {

//ssdp stuff
var ssdpAddres          = "239.255.255.250"
var ssdpPort:UInt16     = 1900
var 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 devices

override 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)

 }
原文链接:https://www.f2er.com/iOS/328961.html

猜你在找的iOS相关文章