gunterhager / udpbroadcastconnection Goto Github PK
View Code? Open in Web Editor NEWFramework to send UDP broadcast messages and listen to responses using a Dispatch source.
License: MIT License
Framework to send UDP broadcast messages and listen to responses using a Dispatch source.
License: MIT License
It seems to me that the instance of UDPBroadcast connection takes some time to be disposed of. I'm using the framework to broadcast a UDP OSC message to an XR18 console. The console replies with its IP address, and other information. This is all working fine. The problem is when I disable WiFi, send the command, re-enable WiFi, send the command again, the socket still seems to be tied up with the previous 'No WiFi' attempt. I've tried to dispose of the 'old' connection like this:broadcastConnection.closeConnection(true) broadcastConnection = nil
and then creating another: broadcastConnection = UDPBroadcastConnection(port: Config.Ports.broadcast) { (ipAddress: String, port: Int, response: [UInt8]) -> Void in let log = "Received from \(ipAddress):\(port):\n\n\(response)" print(log) }
Even though WiFi is connected, and all is as it should be, the returned result is still nil, until I close the viewController, or wait for around 90 seconds. Is there a better way to approach this? Any help would be much appreciated!
Hi Gunter,
I'm using your UDPBroadcastConnection to stream messages at 30-60 messages per second, but notice a performance impact on certain networks. Is it possible to switch from broadcast to unicast after I've found my client IP? Do you think that would improve my performance issue?
Sorry, I posted this request for advice as an issue, it is not an issue. Didn't know where else to ask.
Thank you!
Hi,
I am trying to use the example code, but am getting the following error when I try to invoke sendBroadcast
:
"UDP connection failed to send data: No route to host"
sendingMessageFailed(code: 65)
"Closing UDP socket"
In my app I have:
import SwiftUI
// ...
struct ContentView: View {
// ...
var conn: UDPBroadcastConnection
// ...
init () {
// ...
conn = try! UDPBroadcastConnection(
port: 9012,
handler: { (ipAddress, port, response) -> Void in
print("Received from \(ipAddress):\(port):\n\n\(response)")
},
errorHandler: { (error) in
print(error)
})
}
var body: some View {
// ...
Button(action: {
// ...
do {
try conn.sendBroadcast("This is a test!")
} catch {
print(error)
}
}, label: {
Text("Send")
.padding([.top, .bottom], 6)
.padding([.leading, .trailing], 18)
.background(Color.green)
.foregroundColor(.white)
})
// ...
}
}
This happens regardless of which port I try to use, and it happens even though I have set a string value for NSLocalNetworkUsageDescription
in Info.plist
and I say "OK" when I am prompted that my app "would like to find and connect to devices on your local network." along with the message that I set in Info.plist
.
I am running the app on an iPhone X with iOS 14.4.2. I am connected to a WiFi network.
Hey,
i am looking for a simple way to receive udp data from a remote udp server im my LAN. I tried to modify this project to get it working with another IP Address targeting my broadcast connection, but unfortunately I failed.
Did you ever thought about it?
Hello,
Sometimes I receive this error:
"UDP connection failed to send data: Can't assign requested address"
I think it is because I am changing to a different wifi network while I send packets. Is it related?
Maybe I have to recreate the socket when I see that kind of error.
Thanks.
First of all, thank you for a great functional framework! I was wondering what the best way of dealing with replies from more than one device on the same network. I'm sending a UDP Broadcast on port 10023, and would expect to get more than one reply if there is more than one device on the same network, and port. I'd like to display the results in a table. The device in question replies with quite a bit of information in the response. Will separate replies be received sequentially, but parse correctly? I now that this we be OK if the reply was a struct, but it seems to be three items (IP, port, response). To display these correctly in a table, they would need to arrive in a 'group' - do you know if that would be the case?
Sorry if this is a stupid question!
Regards
John M
hi how can set ip to this library
thanks
Hi,
Thank you for the latest update and this what seems to be a nice library 👍
I'm trying to send a broadcast message on a specific port and get back a response with the ipadresse and port.
do {
broadcastConnection = try UDPBroadcastConnection(
port: UInt16(30303),
handler: { [weak self] (ipAddress: String, port: Int, response: [UInt8]) -> Void in
guard let self = self else { return }
self.log("Received from \(ipAddress):\(port):\n\n\(response)\n")
},
errorHandler: { [weak self] (error) in
guard let self = self else { return }
self.log("Error: \(error)\n")
})
} catch {
log("Error: \(error)\n")
}
` do {
try broadcastConnection.sendBroadcast("toto")
log("Sent: envoyé")
} catch {
log("Error:")
}`
but I don't get a response with ipAdress. do you have any idea about what can be the problem ? is it because of my accessory whose not responding even if I see that he receives the messagE ?
Thank you
Can you tell, how to setup particular outgoing port socket? Every time app create random outgoing port, but I need to use my own.
Hello dear @gunterhager,
are there any updates soon for supporting Swift 3?
thanks.
hello @gunterhager,
What are the chances you could post rewritten code to send 8-bit hex data instead of a string?
I have implemented an M-Search with the package, and it works fine in the Playground, but not when I copy the code in my Mac app.
Is there anything to add in the info.plist or elsewhere ? I have already added the NSLocalNetworkDescription.
The broadcast is sent correctly, and the response is also sent, but the port is unreachable according to Wireshark.
Does anyone have an idea ?
This is my implementation of the class you created.
let broadcastConnection = UDPBroadcastConnection(port: 15600, handler: nil)
let data = Data(base64Encoded: "5555 1234", options: Data.Base64DecodingOptions.ignoreUnknownCharacters)
broadcastConnection.sendBroadcast(data!)
But I'm getting an error in the terminal
"UDP connection failed to send data: Can\'t assign requested address"
"Closing UDP socket"
How can I solve it?
Hi,
got an error when running cert update:
error: SWIFT_VERSION '5.0' is unsupported, supported versions are: 3.0, 4.0, 4.2. (in target 'UDPBroadcast macOS')
any way to resolve this?
Thanks
`
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)")
// it's empty
},
errorHandler: { (error) in
print(error)
})
`
Hi, I have been using this library in one of our apps, It's been working fine, recently I updated the app, everything worked good in Debug Mode but after releasing the app to the App Store, our app users started complaining that it's not working. Upon investigating I found out that ipAddress parameter in the handler is empty when app is running on Release Mode. Could anyone please look into this? @gunterhager , @kenji21
Edit : Just for clarity, everything else is working fine, it broadcasts the message and receive responses from the devices available but just ipAddress field is returning empty in Release mode
Edit 2 : I just found out, this issue happens when the app is built on M series chip of Macs, everything works fine on Intel chip macs
Thank you
Nice UDP Implementation, simple and enough and I'm using it in a new app. A feature that could be convenient would be a timeout after sending broadcasting. In my app I send data, but if my target device is off, no response, then the app just does nothing. Of course I will implement a timeout feature on the app and considering it on the main lib, maybe ;)
When trying to send data to all available devices using udp broadcast, it gives me this log message:
“Error: sendingMessageFailed(code: 22)\n”]
“Closing UDP socket”]
Hello,
I am using UDPBroadcastConnection successfully from an IOS client, to send a message to a local server running in Java. The server receives perfectly well the message, get the client IP address thanks to it, and send back an answer to the client. The handler of the client receives the answer, and identify the IP address of the server thanks to it. No issue.
Once UDPBroadcastConnection has received the answer, will it continue to send broadcast messages, or will it send messages only to the server that answered?
How could I simply modify the code to send only UDP messages to the server IP address, when the "discovery" routin is done?
I am really a beginner concerning swift and UDP sockets, I barely understand 10% of the UDPBroadcastConnection code, so I would appreciate some help :)
Thanks!
As mentioned in #10 functions should throw when an error occurs.
I can't get this framework to work on a real Apple Watch (series 1, WatchOS 6.3). It works on a simulator but not on a real Apple Watch.
I recently downloaded Xcode 9 GM so can migrate code across to Swift 4. With the latest Xcode, I am now getting the following warning
Swift Language Runtime: Simultaneous accesses to 0x60c00027a210, but modification requires exclusive access
The framework still works, but every time UDP broadcast is called, another one of these warnings pops up. Is there any chance the framework will be updated anytime soon to resolve this issue?
we Use this for UDP Scanning and we want timeout for a specific ip after some time
public init(port: UInt16, handler: ((_ ipAddress: String, _ port: Int, _ response: [UInt8]) -> Void)?) {
self.address = sockaddr_in(
sin_len: __uint8_t(MemoryLayout<sockaddr_in>.size),
sin_family: sa_family_t(AF_INET),
sin_port: UDPBroadcastConnection.htonsPort(port: port),
sin_addr: INADDR_BROADCAST,
sin_zero: ( 0, 0, 0, 0, 0, 0, 0, 0 )
)
I'm connected to a device specific wifi (something like arduino)
I'm sending data packet, and I should receive as response from the device the packet id.
The packet I'm sending is 6byte long, the response packet is 1 byte long.
In the Xcode console log I noted something strange:
"UDP connection sent 6 bytes" <--- This is ok
()
"UDP connection received 6 bytes from 10.10.10.110:4150" <--- WTF?
"UDP connection received 1 bytes from 10.10.10.10:4150" <--- This is ok
10.10.10.10 is the "remote" device,
10.10.10.110 is my computer/iphone inside its network.
Why am I receiving 6 byte also from localhost?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.