Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我正在寻找GCDAsyncUdpSocket上的一些示例,但没有找到一个有效的_Ios_Swift_Cocoaasyncsocket - Fatal编程技术网

Ios 我正在寻找GCDAsyncUdpSocket上的一些示例,但没有找到一个有效的

Ios 我正在寻找GCDAsyncUdpSocket上的一些示例,但没有找到一个有效的,ios,swift,cocoaasyncsocket,Ios,Swift,Cocoaasyncsocket,didReceiveData从未被调用-我编写了这个swift类,该类由我的主线UIViewController调用,用于向接收到它的服务器发送消息,但当服务器发回响应时,客户端从未收到它,因为didReceiveData()从未被触发 我一直在谷歌上搜索并查看文档,它说客户端不需要绑定(只有服务器需要这样做),有人能帮我吗?提前谢谢 import UIKit import CocoaAsyncSocket class UdpSocketSR: NSObject, GCDAsyncUdpS

didReceiveData从未被调用-我编写了这个swift类,该类由我的主线UIViewController调用,用于向接收到它的服务器发送消息,但当服务器发回响应时,客户端从未收到它,因为didReceiveData()从未被触发

我一直在谷歌上搜索并查看文档,它说客户端不需要绑定(只有服务器需要这样做),有人能帮我吗?提前谢谢

import UIKit

import CocoaAsyncSocket


class UdpSocketSR: NSObject, GCDAsyncUdpSocketDelegate {


var socket:GCDAsyncUdpSocket!

var rc : Int = 0
var messageOut : String = ""
var messageIn : String = ""


override init(){
    super.init()
}


func SetupAndSend(IP: String, PORT: Int, DATA : String) -> Int
{
    socket = GCDAsyncUdpSocket(delegate: self, delegateQueue: DispatchQueue.main)


    messageOut = DATA
    do {
        let data = messageOut.data(using: String.Encoding.utf8)

        socket.send(data!, toHost: IP, port: UInt16(PORT), withTimeout: 3, tag: 0)

        try socket.beginReceiving()
        sleep(3)
        socket.close()

    } catch {
        rc = -1
    }

    return rc
}



private func udpSocket(sock:GCDAsyncUdpSocket!,didConnectToAddress data : NSData!){
     rc = -2
}

private func udpSocket(sock: GCDAsyncUdpSocket!, didReceiveData dataRecv: NSData!, fromAddress address: NSData!, withFilterContext filterContext: AnyObject!) {

    messageIn = NSString(data: dataRecv as Data, encoding: String.Encoding.utf8.rawValue) as! String

} 


}

为什么在发送后关闭插座?这对我来说很管用

class UdpSocketSR: GCDAsyncSocket, GCDAsyncUdpSocketDelegate {
    var socket: GCDAsyncUdpSocket!

    func SetupAndSend() {
       let host = "127.0.0.1" // IP
       let port: UInt16 = 1234   // Port
       let message = messageOut.data(using: String.Encoding.utf8)!

       socket = GCDAsyncUdpSocket(delegate: self, delegateQueue: DispatchQueue.main)

       do {
           try socket.bindToPort(port)
           try socket.enableBroadcast(true)
           try socket.beginReceiving()
           socket.send(message, toHost: host, port: port, withTimeout: 2, tag: 0)
       }
    }

   // Delegate
   func udpSocket(_ sock: GCDAsyncUdpSocket, didNotConnect error: Error){
      print("UDP Connection error: \(error)")
   }

   func udpSocket(_ sock: GCDAsyncUdpSocket, didReceive data: Data, fromAddress address: Data, withFilterContext filterContext: Any?) {
        var host: NSString?
        var port: UInt16 = 0
        GCDAsyncUdpSocket.getHost(&host, port: &port, fromAddress: address)
        print(host)
    }
}

为什么在发送后立即关闭插座?我删除了socket.close(),但它仍然没有接收数据-错误在于回调函数-我将其编码为didReceiveData而不是didReceive,它可以工作-感谢您的帮助。感谢您对示例代码的帮助。为什么文档说,如果是服务器端的,那么您只需要绑定,而客户端不需要绑定。对吗?顺便说一句,我现在使用了你的didReceive(我用了didReceiveData)回调函数。非常感谢你的帮助。