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

Code Block swift
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

Code Block swift
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



I will add: I did put together a quick test in Go and I am able to receive the UDP packets being broadcasted in the Go client.

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.

So, which way do you have it configured? To broadcast? Or to unicast?

If you’re having problems with broadcast, try unicast.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Hi Quinn:

I've actually just solved it. I needed to call newConnection.start(queue: .main) after configuring my stateHandler. I also need to close my connection after receiving data. Doing this and the data flows. I still don't think I'm receiving my full packets worth of data, but at least I'm not getting a socket error.
Listening for UDP "connection" gives me an error
 
 
Q