.NET MAUI Dns.GetHostEntry()/Socket.Connect() exception on Android 10 and Android Emulator API 27

Rodney (Rod) Girod 1 Reputation point
2022-10-04T20:05:27.55+00:00

I have a .NET MAUI implementation of System.Net.Sockets that runs fine on Windows, but when run on Android 10, the call to Dns.GetHostEntry("GAI-W10-Dev1"); throws an exception: System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: 'hostname nor servname provided, or not known'.

An attempt to skip the GetHostEntry call by just providing the name and port to Socket.Connect failed in the same manner.

The Android device is connected to Wi-Fi and is able to browse the web, so the network stack is fully operational.

What am I missing that is preventing Android from executing the socket calls correctly?

I am building with VS 2022 17.3.2

My Android manifest is as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>

And my C# socket code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace MauiApp1
{
internal class YesaSocket
{
internal IPAddress serverAddress = null;
IPEndPoint serverIpe = null;
Socket server = null;

    internal bool Open()  
    {  
        bool bReturn = false;  
          
        while (true)  
        {  
            IPHostEntry hostEntry = Dns.GetHostEntry("GAI-W10-Dev1");  
            if (hostEntry == null)  
                break;  
            foreach (IPAddress address in hostEntry.AddressList)  
            {  
                if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)  
                {  
                    serverAddress = address;  
                    break;  
                }  
            }  
            if (serverAddress == null)  
                break;  
            serverIpe = new IPEndPoint(serverAddress, 5150);  
            if (serverIpe == null)  
                break;  
            server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  
            if (server == null)  
                break;  

            server.Connect(serverIpe);  
            if (server.Connected)  
                bReturn = true;  
            break;  
        }  
        return bReturn;  
    }  

    internal bool SendRequest(byte[] pRequest, int iRequestSize) {   
        return true;  
    }  

    internal void Close()  
    {  
        server.Close();  
        return;  
    }  
}  

}

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,844 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Rob Caplan - MSFT 5,412 Reputation points Microsoft Employee
    2022-10-25T00:08:03.417+00:00

    @Rodney (Rod) Girod , I suspect the problem is that Android's DNS is set up to go to the public DNS server (likely Google's 8.8.8.8) which doesn't know anything about local addresses. If you tag the local name with the ".local" domain extension (i.e. "gai-w10-dev1.local" instead of gai-w10-dev1) then it should bypass the external DNS and look locally:

     ginna:/ $ ping -c 2 gai-w10-dev1.local  
    

    and

    IPHostEntry hostEntry = Dns.GetHostEntry("GAI-W10-Dev1.local");