Search code examples
xamarin.formstcpclientsocketexception

Xamarin.Forms app TcpClient Error on Connect with "No route to host" exception


I am having very strange issue. I have 2 application, one is just my proof of concept, the other one is my real app. Both are Xamarin.Forms applications supporting Android, iOS, and UWP.

Both of these apps use TcpClient to connect to my IP and Port# on another machine. But here is what happens:

  1. My proof-of-concept app works fine, it can send and receive data to/from that IP/Port#
  2. I also use app TerminalEmulator on Android and Ping on iOS which allow you to ping the IP address. Both devices are able to ping the IP meaning the IP is visible to the devices
  3. But, if I try to connect from my real app on same devices, I get "No route to host" exception

How come my POC app can connect, I can ping the host from my Android/IOS device, but I cannot connect from my real app and get this error:

enter image description here

This is how I try to connect

public override async Task<IList<RoomGuestModel>> GetRoomGuestAsync(string roomNumber)
{
    using (TcpClient client = new TcpClient())
    {
        try
        {
            client.Connect(_tcpConnection.Address, _tcpConnection.Port);
        } 
        catch(Exception ex)
        {

        }
        ...
        ...
        // use NetworkStream to write message and read message
        await myStream.WriteAsync(...);
        ...
        await myStream.ReadAsync(...);

Solution

  • The problem was that I was using wrong IP address, once I provided correct address, I was able to connect