Post

Replies

Boosts

Views

Activity

Listening for UDP "connection" gives me an error
Hi: I'm new to using Network.framework so I concede that my issue might be something really silly. I'm currently attempting to receive game data from F12020 running on another machine. For its telemetry, it has an option to broadcast instead of sending packets to a specific IP. I've written a simple application in the terminal (to test) before integrating it in a proper SwiftUI based application but I'm experiencing an issue. Whenever I recieve a packet from the game, I cannot handle it and I get this error in the console 2020-07-18 17:17:18.260893-0700 FlagManTerminal[39712:4662074] [] nwlistenerinboxacceptudp connect failed [48: Address already in use] When I check lsof -i:20777 I can see a connection between my game computer and my development machine Now I've already enabled (I think correctly) allowLocalEndpointReuse on my NWParameters that I pass to my NWListener. This is what my UDP client looks like import Foundation import Network protocol UDPListener {   func handleResponse(_ client: UDPClient, data: Data) } class UDPClient{   var listener: NWListener?   var port: NWEndpoint.Port   var delegate: UDPListener?       init(port: UInt16){     self.port = NWEndpoint.Port(integerLiteral: port)     startListening()   }       func startListening(){     do{       let params = NWParameters.init(dtls: nil)       params.allowLocalEndpointReuse = true       params.allowFastOpen = true       self.listener = try NWListener(using: params, on: port)       listener?.stateUpdateHandler = { newState in         switch (newState) {           case .ready:             print("State: Ready")             return           case .waiting(let error):             print(error)           case .setup:             print("State: Setup")           case .cancelled:             print("State: Cancelled")           case .failed(let error):             print(error)           default:             print("ERROR! State not defined!\n")         }       }       listener?.newConnectionHandler = { newConnection in         newConnection.stateUpdateHandler = { newState in           switch (newState) {             case .ready:               print("State: Ready")               self.receive(on: newConnection)               return             case .setup:               print("State: Setup")             case .cancelled:               print("State: Cancelled")             case .preparing:               print("State: Preparing")             default:               print("ERROR! State not defined!\n")           }         }       }     }catch{       print("failed to listen")       abort()     }     self.listener?.start(queue: .main)   }       func receive(on connection: NWConnection) {     connection.receiveMessage { data, context, isComplete, error in       guard let data = data else {         print("Error: Received nil Data")         return       }       guard self.delegate != nil else {         print("Error: UDPClient response handler is nil")         return       }       self.delegate?.handleResponse(self, data: data)             }   }     } In my main.swift, I have a simple handler class which I've created, but it's not being used as I don't even get far enough to be calling my handler delegate import Foundation class Flagman: UDPListener {   func handleResponse(_ client: UDPClient, data: Data) {     print(data)   }         } var client = UDPClient.init(port: 20777)     RunLoop.current.run() Is there something really obvious that I'm missing, or can I not receive data from a broadcasted UDP source with Network.framework? Thanks
3
0
1.7k
Jul ’20