我的本地网络中有Wemo belkin设备我正在通过upnp呼叫发送数据,但我无法接收来自wemo设备的响应。 这是我的代码:
class PluginBelkinViewController: UIViewController, GCDAsyncSocketDelegate, GCDAsyncUdpSocketDelegate {
let ssdpAddres = "239.255.255.250"
let ssdpPort:UInt16 = 1900
var ssdpSocket:GCDAsyncUdpSocket!
var ssdpSocketRec:GCDAsyncUdpSocket!
var error: NSError?
@IBAction func nextPressed(sender: UIButton) {
let mSearchData = "M-SEARCH * HTTP/1.1\r\nHOST: \(ssdpAddres):\(ssdpPort)\r\nMAN: \"ssdp:discover\"\r\nMX: 5\r\nST: urn:Belkin:device\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!
do {
ssdpSocket = GCDAsyncUdpSocket(delegate: self, delegateQueue: dispatch_get_main_queue())
ssdpSocket.sendData(mSearchData, toHost: ssdpAddres, port: ssdpPort, withTimeout: 5000, tag: 0)
try ssdpSocket.bindToPort(ssdpPort)
try ssdpSocket.joinMulticastGroup(ssdpAddres)
try ssdpSocket.beginReceiving()
} catch {
print(error)
}
let otherSocket = GCDAsyncSocket(delegate: self, delegateQueue: dispatch_get_main_queue())
do {
try otherSocket.acceptOnPort(8901)
} catch {
print(error)
}
}
func udpSocket(sock: GCDAsyncUdpSocket, didReceiveData data: NSData, fromAddress address: NSData, withFilterContext filterContext: AnyObject?) {
print("--------- didReceiveData ----------")
let str = NSString(data: data, encoding: NSUTF8StringEncoding) as! String
print(str)
}
func socket(socket : GCDAsyncSocket, didReadData data:NSData, withTag tag:Int)
{
print("--------- didReceiveResponse ----------")
let str = NSString(data: data, encoding: NSUTF8StringEncoding) as! String
print(str)
}
}
这是我得到的回应:
--------- didReceiveData ----------
M-SEARCH * HTTP / 1.1
主持人:239.255.255.250:1900
MAN:“ssdp:发现”
MX:1
ST:urn:dial-multiscreen-org:service:dial:1
USER-AGENT:Google Chrome / 54.0.2840.71 Mac OS X
--------- didReceiveData ----------
M-SEARCH * HTTP / 1.1
主持人:239.255.255.250:1900
MAN:“ssdp:发现”
MX:1
ST:urn:dial-multiscreen-org:service:dial:1
USER-AGENT:Google Chrome / 54.0.2840.71 Mac OS X
解决:
我修复了问题,如果有人遇到同样的问题,这是我的完整代码:
class PluginBelkinViewController: UIViewController, GCDAsyncUdpSocketDelegate {
var address = "239.255.255.250"
var port:UInt16 = 1900
var socket:GCDAsyncUdpSocket!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func nextPressed(sender: UIButton) {
let mSearchData = "M-SEARCH * HTTP/1.1\r\nHOST: \(address):\(port)\r\nMAN: \"ssdp:discover\"\r\nMX: 5\r\nST: urn:Belkin:device:\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!
socket = GCDAsyncUdpSocket(delegate: self, delegateQueue: dispatch_get_main_queue())
do {
try socket.bindToPort(0)
try socket.beginReceiving()
try socket.enableBroadcast(true)
} catch {
socket.close()
print(error)
}
socket.sendData(mSearchData, toHost: address, port: port, withTimeout: -1, tag: 0)
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(2 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
self.stopDiscover()
}
}
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)
let str = NSString(data: data, encoding: NSUTF8StringEncoding) as! String
print(str)
}
func stopDiscover(){
socket.close()
socket = nil
}
}