我正在寻找GCDAsyncUdpSocket的一些例子,但发现没有一个可行 [英] I am looking for some examples on GCDAsyncUdpSocket, but found none that works

查看:617
本文介绍了我正在寻找GCDAsyncUdpSocket的一些例子,但发现没有一个可行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

didReceiveData - 我编写了这个由我的主线 UIViewController 调用的swift类,向收到它的服务器发送一条消息,但是当服务器发回响应,客户端永远不会收到它,因为didReceiveData()从未被触发过。

didReceiveData was never called - I coded this swift class which was called by my mainline UIViewController to send out a message to the server which received it OK, but when the server sent a response back, the client never receives it because the didReceiveData() was never triggered.

我一直在谷歌搜索并查看文档,它说客户端不需要绑定(只有服务器需要这样做)任何人都可以帮助我 - 提前谢谢。

I kept googling and look at the doc, and it said that the client does NOT need to bind (Only the server needs doing this) Can anyone help me with this - Thanks in advance.

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

} 


}


推荐答案

为什么在发送后关闭套接字?它适用于绑定。

Why you close your socket after send? It works for me with the bind.

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)
    }
}

这篇关于我正在寻找GCDAsyncUdpSocket的一些例子,但发现没有一个可行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆