我尝试使用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, onPort: ssdpPort, 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.
}
}
答案 0 :(得分:10)
在充分利用代码之后,我已经开始使用它,我在这里发布代码以帮助其他人解决此问题。
当我删除时:
ssdpSocket.connectToHost(ssdpAddres, onPort: ssdpPort, 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, withTimeout: 1, tag: 0)
//bind for responses
ssdpSocket.bindToPort(ssdpPort, error: &error)
ssdpSocket.joinMulticastGroup(ssdpAddres, error: &error)
ssdpSocket.beginReceiving(&error)
}
didReceiveData函数现在被调用,我可以打印响应:
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)
println("From \(host!)")
let gotdata: NSString = NSString(data: data!, encoding: NSUTF8StringEncoding)!
println(gotdata)
}