How to work with UDP sockets in iOS, swift?

前端 未结 2 1854
-上瘾入骨i
-上瘾入骨i 2020-12-30 13:57

I\'m trying connect to local esp8266 UDP server. SwiftSocket haven\'t documentation. CocoaAsyncSocket doesn\'t work. How to connect and send data to udp server? What i shoul

相关标签:
2条回答
  • 2020-12-30 14:26

    This solution work for me! Thanks @Paulw11
    Swift 4, XCode 10.1, iOS 12.0
    Simple connect to the public UDP server (This is NOT optimal version but works):

    import UIKit
    import Network
    
    class ViewController: UIViewController {
    
        var connection: NWConnection?
        var hostUDP: NWEndpoint.Host = "iperf.volia.net"
        var portUDP: NWEndpoint.Port = 5201
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Hack to wait until everything is set up
            var x = 0
            while(x<1000000000) {
                x+=1
            }
            connectToUDP(hostUDP,portUDP)
        }
    
        func connectToUDP(_ hostUDP: NWEndpoint.Host, _ portUDP: NWEndpoint.Port) {
            // Transmited message:
            let messageToUDP = "Test message"
    
            self.connection = NWConnection(host: hostUDP, port: portUDP, using: .udp)
    
            self.connection?.stateUpdateHandler = { (newState) in
                print("This is stateUpdateHandler:")
                switch (newState) {
                    case .ready:
                        print("State: Ready\n")
                        self.sendUDP(messageToUDP)
                        self.receiveUDP()
                    case .setup:
                        print("State: Setup\n")
                    case .cancelled:
                        print("State: Cancelled\n")
                    case .preparing:
                        print("State: Preparing\n")
                    default:
                        print("ERROR! State not defined!\n")
                }
            }
    
            self.connection?.start(queue: .global())
        }
    
        func sendUDP(_ content: Data) {
            self.connection?.send(content: content, completion: NWConnection.SendCompletion.contentProcessed(({ (NWError) in
                if (NWError == nil) {
                    print("Data was sent to UDP")
                } else {
                    print("ERROR! Error when data (Type: Data) sending. NWError: \n \(NWError!)")
                }
            })))
        }
    
        func sendUDP(_ content: String) {
            let contentToSendUDP = content.data(using: String.Encoding.utf8)
            self.connection?.send(content: contentToSendUDP, completion: NWConnection.SendCompletion.contentProcessed(({ (NWError) in
                if (NWError == nil) {
                    print("Data was sent to UDP")
                } else {
                    print("ERROR! Error when data (Type: Data) sending. NWError: \n \(NWError!)")
                }
            })))
        }
    
        func receiveUDP() {
            self.connection?.receiveMessage { (data, context, isComplete, error) in
                if (isComplete) {
                    print("Receive is complete")
                    if (data != nil) {
                        let backToString = String(decoding: data!, as: UTF8.self)
                        print("Received message: \(backToString)")
                    } else {
                        print("Data == nil")
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-30 14:36

    You need to wait until your connection is in the ready state before you try and send or receive any data. You will also need to hold a strong reference to your connection in a property to prevent it from being released as soon as the function exits.

    var connection: NWConnection?
    
    func someFunc() {
    
        self.connection = NWConnection(host: "255.255.255.255", port: 9093, using: .udp)
    
        self.connection?.stateUpdateHandler = { (newState) in
            switch (newState) {
            case .ready:
                print("ready")
                self.send()
                self.receive()
            case .setup:
                print("setup")
            case .cancelled:
                print("cancelled")
            case .preparing:
                print("Preparing")
            default:
                print("waiting or failed")
    
            }
        }
        self.connection?.start(queue: .global())
    
    }
    
    func send() {
        self.connection?.send(content: "Test message".data(using: String.Encoding.utf8), completion: NWConnection.SendCompletion.contentProcessed(({ (NWError) in
            print(NWError)
        })))
    }
    
    func receive() {
        self.connection?.receiveMessage { (data, context, isComplete, error) in
            print("Got it")
            print(data)
        }
    }
    
    0 讨论(0)
提交回复
热议问题