Anonymous
Надежный и правильный способ непрерывного получения данных из UDP-соединения IOS Swift
Сообщение
Anonymous » 24 дек 2025, 09:24
Я пытаюсь создать прослушиватель дрона на своем устройстве и получать данные от дрона через соединение UDP, но проблема в том, что когда я начинаю прослушивать порт, куда дрон отправляет данные, он может прочитать данные только один раз. после этого я получаю кучу журналов, которые не знаю, что это такое.
Я никогда не реализовывал сокет UDP в IOS, поэтому, пожалуйста, помогите мне.
мой текущий код
Код: Выделить всё
import UIKit
import Network
class ViewController: UIViewController {
private var listener: NWListener?
override func viewDidLoad() {
super.viewDidLoad()
startUDPListener()
}
private func startUDPListener() {
do {
let port: NWEndpoint.Port = 14550
listener = try NWListener(using: .udp, on: port)
listener?.stateUpdateHandler = { state in
switch state {
case .ready:
print("UDP Listener ready on port 14550")
case .failed(let error):
print(" Listener failed:", error)
default:
break
}
}
listener?.newConnectionHandler = { [weak self] connection in
print(" New UDP connection")
self?.receive(on: connection)
connection.start(queue: .main)
}
listener?.start(queue: .main)
} catch {
print(" Failed to start UDP listener:", error)
}
}
private func receive(on connection: NWConnection) {
connection.receiveMessage { [weak self] data, context, isComplete, error in
if let data = data, !data.isEmpty {
print("Received \(data.count) bytes")
// Example: raw bytes
print(data as NSData)
// TODO: Decode MAVLink / custom protocol here
}
if error == nil {
self?.receive(on: connection)
}
}
}
deinit {
listener?.cancel()
}
}
журналы
Код: Выделить всё
UDP Listener ready on port 14550
New UDP connection
Received 40 bytes
nw_path_evaluator_create_flow_inner failed NECP_CLIENT_ACTION_ADD_FLOW (null) evaluator parameters: udp, definite, server, attribution: developer, reuse local address, context: Default Network Context (private), proc: 041A3A9F-1BE2-3236-A958-3B4930D1B4C2, local address: 0.0.0.0:14550
nw_path_evaluator_create_flow_inner NECP_CLIENT_ACTION_ADD_FLOW 464C637F-0893-4191-92AA-B82E76CB684E [17: File exists]
nw_endpoint_flow_setup_channel [C2 192.168.4.1:14550 initial channel-flow (satisfied (Path is satisfied), viable, interface: en0[802.11], ipv4, dns, uses wifi)] failed to request add nexus flow
nw_endpoint_flow_failed_with_error [C2 192.168.4.1:14550 initial channel-flow (satisfied (Path is satisfied), viable, interface: en0[802.11], ipv4, dns, uses wifi)] already failing, returning
nw_endpoint_handler_create_from_protocol_listener [C2 192.168.4.1:14550 failed channel-flow (satisfied (Path is satisfied), viable, interface: en0[802.11], ipv4, dns, uses wifi)] nw_endpoint_flow_pre_attach_protocols
nw_connection_create_from_protocol_on_nw_queue [C2] Failed to create connection from listener
nw_ip_channel_inbox_handle_new_flow nw_connection_create_from_protocol_on_nw_queue failed
{length = 40, bytes = 0xfd1c0000 3f01011e 0000f3cf 1600acb7 ... 38ba98dc 543b049a }
nw_path_evaluator_create_flow_inner failed NECP_CLIENT_ACTION_ADD_FLOW (null) evaluator parameters: udp, definite, server, attribution: developer, reuse local address, context: Default Network Context (private), proc: 041A3A9F-1BE2-3236-A958-3B4930D1B4C2, local address: 0.0.0.0:14550
nw_path_evaluator_create_flow_inner NECP_CLIENT_ACTION_ADD_FLOW 464C637F-0893-4191-92AA-B82E76CB684E [17: File exists]
nw_endpoint_flow_setup_channel [C3 192.168.4.1:14550 initial channel-flow (satisfied (Path is satisfied), viable, interface: en0[802.11], ipv4, dns, uses wifi)] failed to request add nexus flow
nw_endpoint_flow_failed_with_error [C3 192.168.4.1:14550 initial channel-flow (satisfied (Path is satisfied), viable, interface: en0[802.11], ipv4, dns, uses wifi)] already failing, returning
nw_endpoint_handler_create_from_protocol_listener [C3 192.168.4.1:14550 failed channel-flow (satisfied (Path is satisfied), viable, interface: en0[802.11], ipv4, dns, uses wifi)] nw_endpoint_flow_pre_attach_protocols
nw_connection_create_from_protocol_on_nw_queue [C3] Failed to create connection from listener
nw_ip_channel_inbox_handle_new_flow nw_connection_create_from_protocol_on_nw_queue failed
nw_path_evaluator_create_flow_inner failed NECP_CLIENT_ACTION_ADD_FLOW (null) evaluator parameters: udp, definite, server, attribution: developer, reuse local address, context: Default Network Context (private), proc: 041A3A9F-1BE2-3236-A958-3B4930D1B4C2, local address: 0.0.0.0:14550
nw_path_evaluator_create_flow_inner NECP_CLIENT_ACTION_ADD_FLOW 464C637F-0893-4191-92AA-B82E76CB684E [17: File exists]
Я получаю всю эту кучу журналов, но не данных??
Подробнее здесь:
https://stackoverflow.com/questions/798 ... ion-ios-sw
1766557451
Anonymous
Я пытаюсь создать прослушиватель дрона на своем устройстве и получать данные от дрона через соединение UDP, но проблема в том, что когда я начинаю прослушивать порт, куда дрон отправляет данные, он может прочитать данные только один раз. после этого я получаю кучу журналов, которые не знаю, что это такое. Я никогда не реализовывал сокет UDP в IOS, поэтому, пожалуйста, помогите мне. мой текущий код [code] import UIKit import Network class ViewController: UIViewController { private var listener: NWListener? override func viewDidLoad() { super.viewDidLoad() startUDPListener() } private func startUDPListener() { do { let port: NWEndpoint.Port = 14550 listener = try NWListener(using: .udp, on: port) listener?.stateUpdateHandler = { state in switch state { case .ready: print("UDP Listener ready on port 14550") case .failed(let error): print(" Listener failed:", error) default: break } } listener?.newConnectionHandler = { [weak self] connection in print(" New UDP connection") self?.receive(on: connection) connection.start(queue: .main) } listener?.start(queue: .main) } catch { print(" Failed to start UDP listener:", error) } } private func receive(on connection: NWConnection) { connection.receiveMessage { [weak self] data, context, isComplete, error in if let data = data, !data.isEmpty { print("Received \(data.count) bytes") // Example: raw bytes print(data as NSData) // TODO: Decode MAVLink / custom protocol here } if error == nil { self?.receive(on: connection) } } } deinit { listener?.cancel() } } [/code] журналы [code]UDP Listener ready on port 14550 New UDP connection Received 40 bytes nw_path_evaluator_create_flow_inner failed NECP_CLIENT_ACTION_ADD_FLOW (null) evaluator parameters: udp, definite, server, attribution: developer, reuse local address, context: Default Network Context (private), proc: 041A3A9F-1BE2-3236-A958-3B4930D1B4C2, local address: 0.0.0.0:14550 nw_path_evaluator_create_flow_inner NECP_CLIENT_ACTION_ADD_FLOW 464C637F-0893-4191-92AA-B82E76CB684E [17: File exists] nw_endpoint_flow_setup_channel [C2 192.168.4.1:14550 initial channel-flow (satisfied (Path is satisfied), viable, interface: en0[802.11], ipv4, dns, uses wifi)] failed to request add nexus flow nw_endpoint_flow_failed_with_error [C2 192.168.4.1:14550 initial channel-flow (satisfied (Path is satisfied), viable, interface: en0[802.11], ipv4, dns, uses wifi)] already failing, returning nw_endpoint_handler_create_from_protocol_listener [C2 192.168.4.1:14550 failed channel-flow (satisfied (Path is satisfied), viable, interface: en0[802.11], ipv4, dns, uses wifi)] nw_endpoint_flow_pre_attach_protocols nw_connection_create_from_protocol_on_nw_queue [C2] Failed to create connection from listener nw_ip_channel_inbox_handle_new_flow nw_connection_create_from_protocol_on_nw_queue failed {length = 40, bytes = 0xfd1c0000 3f01011e 0000f3cf 1600acb7 ... 38ba98dc 543b049a } nw_path_evaluator_create_flow_inner failed NECP_CLIENT_ACTION_ADD_FLOW (null) evaluator parameters: udp, definite, server, attribution: developer, reuse local address, context: Default Network Context (private), proc: 041A3A9F-1BE2-3236-A958-3B4930D1B4C2, local address: 0.0.0.0:14550 nw_path_evaluator_create_flow_inner NECP_CLIENT_ACTION_ADD_FLOW 464C637F-0893-4191-92AA-B82E76CB684E [17: File exists] nw_endpoint_flow_setup_channel [C3 192.168.4.1:14550 initial channel-flow (satisfied (Path is satisfied), viable, interface: en0[802.11], ipv4, dns, uses wifi)] failed to request add nexus flow nw_endpoint_flow_failed_with_error [C3 192.168.4.1:14550 initial channel-flow (satisfied (Path is satisfied), viable, interface: en0[802.11], ipv4, dns, uses wifi)] already failing, returning nw_endpoint_handler_create_from_protocol_listener [C3 192.168.4.1:14550 failed channel-flow (satisfied (Path is satisfied), viable, interface: en0[802.11], ipv4, dns, uses wifi)] nw_endpoint_flow_pre_attach_protocols nw_connection_create_from_protocol_on_nw_queue [C3] Failed to create connection from listener nw_ip_channel_inbox_handle_new_flow nw_connection_create_from_protocol_on_nw_queue failed nw_path_evaluator_create_flow_inner failed NECP_CLIENT_ACTION_ADD_FLOW (null) evaluator parameters: udp, definite, server, attribution: developer, reuse local address, context: Default Network Context (private), proc: 041A3A9F-1BE2-3236-A958-3B4930D1B4C2, local address: 0.0.0.0:14550 nw_path_evaluator_create_flow_inner NECP_CLIENT_ACTION_ADD_FLOW 464C637F-0893-4191-92AA-B82E76CB684E [17: File exists] [/code] Я получаю всю эту кучу журналов, но не данных?? Подробнее здесь: [url]https://stackoverflow.com/questions/79854163/reliable-and-correct-way-of-getting-data-continuously-from-udp-connection-ios-sw[/url]