V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Trinity888
V2EX  ›  iOS

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

  •  
  •   Trinity888 · 2021-06-27 15:25:38 +08:00 · 1598 次点击
    这是一个创建于 1247 天前的主题,其中的信息可能已经有所发展或是发生改变。

    现状:一设备( udp client 端)广播出来数据包,10s 一次,APP 收到概率太小,而且如果收到了时间非常慢。 但用测试工具 UDP,是能收到这个设备的数据包;

    // // UDPSocketManager.swift // CSSmartIOS // // Created by 陳烁 on 2021/5/31. // 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) {
        // 发送数据成功
    }
    

    }

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5043 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 09:48 · PVG 17:48 · LAX 01:48 · JFK 04:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.