求教: APP, IOS Swift 写的 UDP 广播模式(作为 Server)udpSocket 接收不到数据问题,请大家帮忙看下哪里的问题

2021-06-26 19:06:28 +08:00
 Trinity888

import SystemConfiguration.CaptiveNetwork import NetworkExtension

import UIKit

class UDPSocketManager: NSObject, GCDAsyncUdpSocketDelegate {

static var shared = UDPSocketManager()

var udpSocket: GCDAsyncUdpSocket?
var udpError: String?
var port = 26600

var isStarting: Bool = false

override init() {
    super.init()
}

func start() {
    if isStarting {
        return
    }
    self.udpSocket = GCDAsyncUdpSocket(delegate: self, delegateQueue: DispatchQueue.main)
    do {
        try udpSocket?.bind(toPort: UInt16(port), interface: udpError)
        try udpSocket?.enableBroadcast(true)
        try udpSocket?.beginReceiving()
        self.isStarting = true
    } catch {
        print("catch:\(String(describing: error))")
        self.isStarting = false
    }
}

func close() {
    self.isStarting = false
    udpSocket?.close()
}

func sendUdp(aaa: String, ssid: String, pw: String, address: Data) {
    let str = "ssid:\(ssid)pw:\(pw)"
    let asciiArr = str.asciiHexValues
    let instructStr = BasicProtocol.gatewayInstructionGeneration(frameNumStrArr: BasicProtocol.gatewayFrameNumStrArr, respFlagStrArr: ["80"], cmdIdStrArr: ["08", "01"], dataStrArr: asciiArr)
    HUD.showSuccess("收到数据:\(aaa)\n 发送数据:\(instructStr)", delay: 5)
    // self.udpSocket.send(Data(instructStr.components(separatedBy: " ").joined().changeToBytes()), toHost: "192.168.4.1", port: UInt16(self.port), withTimeout: -1, tag: 0)

    self.udpSocket?.send(Data(instructStr.components(separatedBy: " ").joined().changeToBytes()), toAddress: address, withTimeout: -1, tag: 0)
    
}


func sendUdp(str: String, address: Data) {
    let asciiArr = str.asciiValues
    let instructStr = BasicProtocol.gatewayInstructionGeneration(frameNumStrArr: BasicProtocol.gatewayFrameNumStrArr, respFlagStrArr: ["80"], cmdIdStrArr: ["08", "01"], dataStrArr: asciiArr)
    HUD.showSuccess("发送数据:\(instructStr)", delay: 5)
    self.udpSocket?.send(Data(instructStr.components(separatedBy: " ").joined().changeToBytes()), toAddress: address, withTimeout: -1, tag: 0)
    
}

func udpSocket(_ sock: GCDAsyncUdpSocket, didReceive data: Data, fromAddress address: Data, withFilterContext filterContext: Any?) {
    // 接受数据
    let strData = data.map({ String(format: "%02x", $0) }).joined(separator: " ")
    HUD.showSuccess("收到数据:\(strData)", delay: 5)
    
    NotificationCenter.default.post(name: NSNotification.Name("UDPSocketNewMessage"), object: ["strData": strData, "address": address])
}

func udpSocket(_ sock: GCDAsyncUdpSocket, didSendDataWithTag tag: Int) {
    // 发送数据成功
}

}

1222 次点击
所在节点    Swift
1 条回复
Trinity888
2021-06-26 19:08:42 +08:00
客户端 UDP 广播包:每 30s 发过来一次,以上是 APP 端接收。

现状是:IOS 端接收不稳定,并且接收到数据时间很长。有时候能接收到 1 条,回执后,就再收不到客户端 UDP 广播包,第二条数据了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/785970

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX