How to Select Specific Network Interface for Socket Connections in MAUI iOS
I am trying to create a socket connection from Android/iOS that will connect to a ESP32 access point although MAUI keeps indicating "No Route to Host" when trying to open the connection. My understand of this is the host responded with that this request does not have a valid 'destination', which is expected if the requested IP address does not exist.
This access point (ESP) does not have an internet connection, it is purely used for connecting to a smartphone or PC to allow sending commands via a socket connection.
After connecting to the ES32 Wifi from the phone settings, I am able to use any free TCP applications on both Android and iOS to open a socket connection to 192.168.4.1 on port 23. Once the connection is opened, I am able to send and receive information happily.
I am able to use the code below on an android and the socket connection opens fine. When I use it on iOS, I get back a "No Route to Host" error. Considering I am able to connect using a different application on the same iphone, I know its not the ESP failing here.
CancellationToken CToken = new CancellationToken();
using Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
await socket.ConnectAsync(IPAddress.Parse("192.168.4.1"), 23, CToken);
When connected to the ESP32 and looking at the wifi settings off the iphone, we have:
IP Address: 192.168.4.2
Subnet Mask: 255.255.255.0
Router: 192.168.4.1
DNS (is on Automatic): 192.168.4.1
Now here comes the next confusing part. A work colleague is able to use his iphone using the exact same EP32 access point, and able to connect a socket with the exact three lines of code above using MAUI. Both phones are identical (iphone 12 pro) and running the exact same iOS firmware 16.1.2
The only thing I can kind of figure that may be a difference, is the iphone is selecting the incorrect network adapter. So instead of using the ESP32 wifi connection, its possibly trying to connect a socket from one of the other network interfaces? Such as cellular?
Does anyone know how to enforce using a specific network adaptor for iOS? Even if its iOS platform specific code that needs to be run to setup/enforce at the beginning? Or have any other suggestions?