Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

send receive udp swift

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

PREVIOUS NEXT
Code Example
Swift :: Swift Assignment Operators 
Swift :: Swift enums with rawValue 
Swift :: swift from 1 to 01 
Swift :: swift protocol inheritance 
Swift :: set color of indicator line in collectionview swift 
Swift :: Swift Bitwise XOR Operator 
Swift :: Logical Operators Swift 
Swift :: swift loop through array of objet 
Swift :: swift 5 progress bar height 
Swift :: store multiple items in one core data record 
Swift :: implement swift protocol in kotlin 
Swift :: rust How to pass out parameter to function from Swift FFI 
Swift :: swiftui polygon 
Swift :: single word search swift 
Swift :: swiftui profile picture 
Swift :: Swift Comparison Operators 
Swift :: how to use snippets in xcode 
Swift :: caseiterable swift 
Swift :: swift collectionview ispagingenabled change page size 
Swift :: Swift Nested for Loop 
Swift :: swift allow gesture recognizer over others 
Ruby :: create rails app with postgres 
Ruby :: rails prepare testing db 
Ruby :: ruby is int 
Ruby :: ruby loop from the last array item 
Ruby :: rails foreach 
Ruby :: ruby substring remove 
Ruby :: how to make a new array ruby 
Ruby :: ruby typeof 
Ruby :: rails render head: :ok 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =