swift - CocoaAsyncSocket: Error creating iOS UDP socket when dispatch_sync(socketQueue, block) is called -
i write ios swift app connects udp server locally.
here code wrote using library called cocoaasyncsocket, when run it crash.
i haven't found similar high level api in apple. thing found documentation suggest higher level api.
would able point me towards better library or towards resolving bug get?
import uikit import cocoaasyncsocket class outputsocket: nsobject, gcdasyncudpsocketdelegate { var serverip = "127.0.0.1" var port:uint16 = 10000 var socket:gcdasyncudpsocket! override init(){ super.init() setupconnection() } func setupconnection(){ socket = gcdasyncudpsocket(delegate: self, delegatequeue: dispatchqueue.main) { try socket.bind(toport: self.port) } catch { print("binding error: ", error.localizeddescription) } let addressdata = serverip.data(using: string.encoding.utf8) { try socket.connect(toaddress: addressdata!)} catch { print("connecting error: ", error.localizeddescription) } { try socket.beginreceiving() } catch { print("connecting error: ", error.localizeddescription) } } func send(message:string){ let data = message.data(using: string.encoding.utf8) let addressdata = serverip.data(using: string.encoding.utf8) socket.send(data!, toaddress: addressdata!, withtimeout: 2, tag: 0) } func udpsocket(_ sock: gcdasyncudpsocket, didconnecttoaddress address: data) { print("didconnecttoaddress"); } func udpsocket(_ sock: gcdasyncudpsocket, didnotconnect error: error?) { print("didnotconnect \(error)") } func udpsocket(_ sock: gcdasyncudpsocket, didsenddatawithtag tag: int) { print("didsenddatawithtag") } func udpsocket(_ sock: gcdasyncudpsocket!, didnotsenddatawithtag tag: int, duetoerror error: error!) { print("didnotsenddatawithtag") } }
error description:
when run it crashes here:
edit:
i have removed ip address equation , now, when run following code "didnotsenddatawithtag":
let data = message.data(using: string.encoding.utf8) socket.send(data!, withtimeout: 2, tag: 0)
any idea why?
Comments
Post a Comment