Framework to send UDP broadcast messages and listen to responses using a Dispatch dispatch source.
I think that modern apps should no longer use this package. Please look into Apple's NWConnection
instead.
iOS 11+, Swift 5.0
An example app is included demonstrating UDPBroadcastConnection's functionality. The example probably won't work for you out of the box since you need someone to listen and respond on the correct UDP port on your network.
Create a UDPBroadcastConnection
on port 35602
with a closure that handles the response:
broadcastConnection = try UDPBroadcastConnection(
port: 35602,
handler: { (response: (ipAddress: String, port: Int, response: [UInt8])) -> Void in
print("Received from \(response.ipAddress):\(response.port):\n\n\(response.response)")
},
errorHandler: { (error) in
print(error)
})
Note: Make sure to keep a strong reference to broadcastConnection
(e.g. by storing it in a property) to be able to receive responses.
Send a message via broadcast:
try broadcastConnection.sendBroadcast("This is a test!")
You can test the broadcast and the handler for receiving messages by running the included receive_and_reply.py
script (tested with Python 2.7.10) on your Mac. If you send a broadcast with the example app, you should see the message that was sent in the terminal and see the script's answer in the example app.
Add the following line to your Cartfile.
github "gunterhager/UDPBroadcastConnection"
Then run carthage update
.
Just drag and drop the .swift
files in the UDPBroadcastConnection
folder into your project.
UDPBroadcastConnection
is available under the MIT license. See the LICENSE file for details.
Made with ❤ at all about apps.