Framework to send IPv4/IPv6 UDP broadcast messages and listen to responses using a Dispatch dispatch source.
Note: this is a fork of gunterhager/UDPBroadcastConnection with breaking changes:
- Immediate binding is not supported
- carthage support was removed
iOS 9.3+, 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 an UDPv4BroadcastConnection
on port 35602
with a closure that handles the response:
broadcastConnection = try UDPv4BroadcastConnection(
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!")
There is also support for link-local multicasts to fe02::1
via UDPv6BroadcastConnection
. This needs to be scoped to a network interface. Usually en0
is the
right choice, however you should figure out programmatically the default network interface (i.e. via the NWPathMonitor
class in the Network framework).
This example sets up two connections, one for IPv4 and one for IPv6:
broadcastv4Connection = try UDPBroadcastConnection(
addressFamily: .ipv4,
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)
})
broadcastv6Connection = try UDPBroadcastConnection(
addressFamily: .ipv6,
interface: "en0",
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)
})
Send a messages via broadcast:
try broadcastv4Connection.sendBroadcast("This is an IPv4 test!")
try broadcastv6Connection.sendBroadcast("This is an IPv6 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.
Create or modify the Package.swift at the root folder of your project. You can use the automatic linking mode (static/dynamic), or use the project UDPBroadcastConnectionDynamic
to force dynamic linking and overcome current Xcode limitations to resolve diamond dependency issues.
If you use it from only one target, automatic mode should be fine.
Automatic linking mode:
// swift-tools-version:5.2
import PackageDescription
let package = Package(
name: "MyApp",
products: [
.executable(name: "MyApp", targets: ["MyApp"])
],
dependencies: [
.package(url: "https://github.com/teufelaudio/UDPBroadcastConnection.git", .branch("master"))
],
targets: [
.target(name: "MyApp", dependencies: ["UDPBroadcastConnection"])
]
)
Dynamic linking mode:
// swift-tools-version:5.2
import PackageDescription
let package = Package(
name: "MyApp",
products: [
.executable(name: "MyApp", targets: ["MyApp"])
],
dependencies: [
.package(url: "https://github.com/teufelaudio/UDPBroadcastConnection.git", .branch("master"))
],
targets: [
.target(name: "MyApp", dependencies: ["UDPBroadcastConnectionDynamic"])
]
)
Then you can either build on the terminal or use Xcode 11 or higher that now supports SPM natively.
$ swift build
$ xed .
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.