Reputation: 43
I have an app that requires a to communicate with another device using UDP
.
I am using the GCDAsyncUDPSocket
framework.
I do not know the port that it will be required to send back over until I first receive on port 14550.
To get around this I implement 2 different sockets, a transmit socket and a receive socket.
First I initialise both sockets and open port 14550 on the receive socket.
func wifiConnect() {
receiveSocket = GCDAsyncUdpSocket(delegate: self, delegateQueue: dispatch_get_main_queue())
transmitSocket = GCDAsyncUdpSocket(delegate: self, delegateQueue: dispatch_get_main_queue())
do {
try receiveSocket!.bindToPort(14550)
try transmitSocket!.enableBroadcast(true)
try receiveSocket!.beginReceiving()
} catch _ as NSError { print("Issue with setting up listener") }
}
Then I wait until data is received on that port and then I connect to the sender address for the transmit socket.
func udpSocket(sock: GCDAsyncUdpSocket, didReceiveData data: NSData, fromAddress address: NSData, withFilterContext filterContext: AnyObject?) {
if !udpConneced {
do {
try transmitSocket?.connectToAddress(address)
} catch let error {
NSLog("Error connecting to address. %@",String(error))
return
}
}
dataReceived(data)
}
Once connected udpConnected
is set to true and a function is ran.
func udpSocket(sock: GCDAsyncUdpSocket, didConnectToAddress address: NSData) {
udpConnected = true
didConnect()
}
However, sometimes the entire app is crashing and giving this error
NSURLConnection finished with error - code -1002
Which I understand to be an invalid URL, however I am not passing any urls myself to either socket.
What is causing this issue? I do know the IP of the connecting device so should I be using that to connect the transmit socket?
Thanks!
Upvotes: 0
Views: 6040
Reputation: 43
Solved this. Turns out testflight was not reporting showing the full crash log. I managed to cause the crash myself and saw that it was nothing to do with NSURL or GCDASYNC. It was caused by a variable being nil whilst being called in a rare situation.
Upvotes: 2