大佬教程收集整理的这篇文章主要介绍了如何通过iOS中的套接字从服务器接收数据?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
var socket = AsyncSocket() socket = AsyncSocket(delegate: self) self.socketConnect() func socketConnect() { do { try socket?.connectToHost("IP Address",onPort: 6968) } catch _ as NSError { } } //MARK: - AsyncSocket Delegates method func onSocket(sock: AsyncSocket!,didConnectToHost host: String!,port: UInt16) { print("Connected to host : \(host) with Port : \(port)") let alert:UIAlertController = UIAlertController(title: "Connected",message: "Host:\(host) ** Port:\(port)",preferredStyle:.Alert) self.presentViewController(alert,animated: true,completion: nil) let action:UIAlertAction = UIAlertAction(title: "Ok",style: .Default) { (UIAlertAction) -> Void in print("Ok Pressed") } alert .addAction(action) let Dict = ["iUserId":"100","iRidEID":"276","type":"client"] // For client side var jsonString = NSString() do { let data = try NSJSONserialization.dataWithJSONObject(Dict,options: NSJSONWriTingOptions.PrettyPrinted) jsonString = NSString(data: data,encoding: NSUTF8StringEncoding)! }catch let error as NSError { print(error) } let reqData = jsonString.dataUsingEncoding(NSUTF8StringEncoding) socket.writeData(reqData,withTimeout: 1.0,tag: 100) } func onSocket(sock: AsyncSocket!,didReadPartialDataOfLength partiALLENgth: UInt,tag: int) { print("Read partial data") } func onSocket(sock: AsyncSocket!,didWriteDataWithTag tag: int) { print("Data write successfully") } func onSocket(sock: AsyncSocket!,didReadData data: NSData!,withTag tag: int) { print("Data read successfully") } func onSocket(sock: AsyncSocket!,willDisconnectWithError err: NSError!) { print("Socket disconnect with error :\(err.description)") } func onSocket(sock: AsyncSocket!,didAcceptNewSocket newSocket: AsyncSocket!) { print("Accept new socket") }
func socket(sock: GCDAsyncSocket!,port: UInt16) { print("Connected to host : \(host) with Port : \(port)") socket.readDataWithTimeout(-1,tag: 0) } func socket(sock: GCDAsyncSocket!,withTag tag: int) { print("Data read successfully") socket.readDataWithTimeout(-1,tag: 0) }
以上是大佬教程为你收集整理的如何通过iOS中的套接字从服务器接收数据?全部内容,希望文章能够帮你解决如何通过iOS中的套接字从服务器接收数据?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。