NIO bind to 0.0.0.0 on macOS does not register connections made to Mac's IP address

I have a macOS app that I'm using SwiftNIO to listen for connections on a port using the recommended 0.0.0.0 address in the example, as below.

      // Bind the port and run the server
      do {
        channel = try bootstrap.bind(host: "0.0.0.0", port: Int(listeningPort)).wait()
        print("Server started and listening on \(channel!.localAddress!)")
      }
      catch let error {
        print("Server failed to start: \(error.localizedDescription)")
        channel = nil
      }

But when I try to connect via telnet to either the Mac's IP address, to 127.0.0.1 or 0.0.0.0 and the port I get a successful connect but no data is transferred. I use this example code on a Linux app with the IP set on the bind call to 0.0.0.0 and can get data from the machines IP address. I set the debugger to break on the channelRegistered and its never paused unless I call bind with the machines actual IP address.

Is there a setting on the Mac I need to ensure to bind it to the one active network interface on the Mac? Do I need to go into SystemConfiguration to grab the IP address and pass that to bind?

TIA

0.0.0.0 should bind all network interfaces. Do you have a Mac firewall enabled? You may need to tweak the firewall rules.

The firewall on the Mac is fully off. I tried enabling it and then explicitly enabling the app, to no change.

This is related to how addresses are resolved on macOS.

I am rusty with this, since I haven't dealt with this for a long time. However, a quick google brought something up like this. There are a lot more results from 2016, 2017, etc.

Having said that, I have connected to local services (Angular as well as Django) via localhost on macOS just recently. So, I assume there is a way to make this work. Since you said telnet, I would also try to open the port with a different tool. For example, a web-browser. It may sound unbelievable, but different tools use different libraries to resolve addresses and this particular problem might be an issue with telnet.

Good luck!

Kim

And in creating a sample project demonstrating the issue, I found that it appears to be port related and found something else running on the port I was using...

2 Likes