I have a swift structure which contains a number of items:
struct ConfigParams {
var MessageID: UInt16 = 0
var SN: UInt16 = 1
var ManufactureData: UInt32 = 0
var Version = [Character](repeating:"?", count:16)
}
And a member function that returns this as a Data type for transmission over the netork:
func asData() -> Data {
var data = Data()
data.append(withUnsafeBytes(of: MessageID) { Data($0)})
data.append(withUnsafeBytes(of: SN) { Data($0)})
data.append(withUnsafeBytes(of: ManufactureData) { Data($0)})
print("My size before \(data.count)")
data.append(withUnsafeBytes(of: Version) { Data($0)})
print("My size after \(data.count)")
print(Version.count)
return data
}
When I call "asData()" it gives a Data() with the wrong count of bytes in it!
The output of calling the above method is:
My size before 8
My size after 16
16
So when I append the contents of Version it it only adding 8 bytes, not 16.
But I would expect the final size to be 24!
What am I missing here?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I am trying to write a UDP client in Swift 5 which sends data to a UDP server which is running on a windows machine. The UDP Server is written in c#
The c# end does this:
IPAddress ipAddress = IPAddress.Parse("192.168.1.228);
IPEndPoint endPoint = new IPEndPoint(ipAddress, 10010);
udpClient = new UdpClient(endPoint);
udpClient.Connect("192.168.1.53, 4001);
udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), null);
And my Swift client is this:
class MeetingMaker {
var connection: NWConnection?
var host: NWEndpoint.Host = "192.168.1.228"
var port: NWEndpoint.Port = 10010
var m2h: MotionToHost = MotionToHost()
var timer = Timer()
func send(_ payload: Data) {
connection!.send(content: payload, completion: .contentProcessed({ sendError in
if let error = sendError {
print("Unable to process and send the data: \(error)")
} else {
print("Data has been sent")
}
}))
}
func startSendingUpdates() {
Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { timer in
self.sendTick()
}
}
func sendTick() {
if connection?.state == .ready {
//print("MB SEND")
send(m2h.asData())
}
}
func connect() {
connection = NWConnection(host: host, port: port, using: .udp)
connection!.stateUpdateHandler = { (newState) in
switch (newState) {
case .preparing:
NSLog("MB: Entered state: preparing")
case .ready:
NSLog("MB: Entered state: ready")
case .setup:
NSLog("MB: Entered state: setup")
case .cancelled:
NSLog("MB: Entered state: cancelled")
case .waiting:
NSLog("MB: Entered state: waiting")
case .failed:
NSLog("MB: Entered state: failed")
default:
NSLog("MB: Entered an unknown state")
}
}
connection!.viabilityUpdateHandler = { (isViable) in
if (isViable) {
NSLog("MB: Connection is viable")
} else {
NSLog("MB: Connection is not viable")
}
}
connection!.betterPathUpdateHandler = { (betterPathAvailable) in
if (betterPathAvailable) {
NSLog("MB: A better path is availble")
} else {
NSLog("MB: No better path is available")
}
}
connection!.start(queue: .global())
}
}
And elsewhere :
meetingMaker.Connect()
And
meetingMaker.StartSendingUpdates()
The issue is that the UDP Server on windows never receives the packets.
I think it expects them to come from source-port 4001?
I cannot find how to bind my Swift UDP CLient to send from a specified local port?
(The IP addresses are fixed and correct).
Is it possible to use Swift to send UDP packets from a specific local port, to the specified destination port? Or is the issue elsewhere?
I'm working on an app which will detect smart-hubs (manufactured by my client) on the local network.
There are a few things I need to do:
Send UDP Packets to a broadcast address.
Receive UDP Packets that have been broadcast to the same address.
Use a NWBrowser to find devices which advertise as _mydevice._tcp.
In the simulator this is all working well.
It will not even build for my development device (iPhone SE running iOS 15.4.1)
I have added the NSBonjourServices array, NSLocalNetworkUsageDescription string, and com.apple.developer.networking.multicast boolean into my entitlements file.
However xCode is unable to update the provisioning profile:
I have tried to follow all Quinn's instructions that lead to this page. but I must be missing something because there is no "Additional Capabilities" tab for my App ID and I cannot see anywhere to request/set the NSBonjour entitlements or udp multicast entitlements for the AppID.
I have tried to read the documentaion but getting nowhere. I have missed a secret step somewhere!
Please help me get this working. It is vital for our app.
I have an app which detects smart-hubs on the network, then for each smart-hub, detects devices conneted to the hub, then each device has a number of channels.
The first view shows all the smart-hubs. Tap on one, and it moves to the next view, which shows all the devices. Tap on a device and it shows all the channels for the chosen device. Each channel is a Slider.
When I touch the slider in the channel-view, the navigation immediately pops back one level to the device-view!
(See diagram below)
Any idea what is causing this?
The first view has a binding to an array of Controllers.
@Binding var controllers: [Controller]
It passes a selected controller to the second view
@Binding var controller: Controller, which passes the selected device to the third view. @Binding var canDevice: CanDevice
What is the correct way to re-write this code:
randomIV = Data(count: 16)
let result = randomIV.withUnsafeMutableBytes {
SecRandomCopyBytes(kSecRandomDefault, 16, $0)
}
To remove this warning:
'withUnsafeMutableBytes' is deprecated: use withUnsafeMutableBytes(_: (UnsafeMutableRawBufferPointer) throws -> R) rethrows -> R` instead