Does TCP Socket Implementation Work Differently in Xamarin.Android?

0

I am trying to make a TCP connection in my Xamarin Android app using System.Net.Sockets.TcpClient.

The connection is perfectly fine when I do it on a console application. However, when I used the exact same code in my android app, I get an exception on tcpClient.Connect("127.0.0.1", 6121); saying "System.Net.Sockets.SocketException (0x80004005): Connection refused". Is there something I need to do differently since it's Xamarin Android? Below is my code. Thank you for the help!

using Android.App;
using Android.OS;
using System;
using System.Net.Sockets;

namespace App1
{
    [Activity(Label = "App1", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            Connect();
        }

        public void Connect()
        {
            try
            {
                TcpClient tcpClient = new TcpClient();
                tcpClient.Connect("127.0.0.1", 6121);
            }
            catch(Exception){}
        }
    }
}
c#
android
xamarin
tcp
tcpclient
asked on Stack Overflow Aug 21, 2019 by cxc

1 Answer

2

Regardless of whether or not you can do network operations on the main thread (as commented by @ÖmerBaş,) you have a much more basic problem with network concepts causing your error.

127.0.0.1 always refers to the machine the program is running on.

When you run your code in the console, 127.0.0.1 refers to the PC you are working on and presumably where the TCP server is running.

When you run the code on Android, 127.0.0.1 refers to the Android device itself. Your error message says "Connection refused." That's because your TCP-Server isn't running on the Android device. It is running on your PC.

You need to know what IP address your Android device can use to connect to your PC.

If you are using a real Android device, you will need to have your Android device connect to the same network as your PC via WiFi. Then you can use your PC's IP address in your code.

For the simulators, there's a couple of possibilities:

  1. Google simulators: 10.0.2.2 is the address of your PC as seen from the Android.
  2. Microsoft simulators: 169.254.80.80 is the address of your PC as seen from the Android.

(Simulator addresses take from here.)

Make sure to bind your TCP service to all IP addresses, not just 127.0.0.1.

An alternative solution is to use adb to set up port forwarding on your Android device (whether simulated or real.)


I think the "network access on the main thread" thing is more a problem of "should not" rather than "cannot." The question referred to by Ömer Baş shows "permission denied" errors rather than something pointing to a threading problem.

answered on Stack Overflow Aug 21, 2019 by JRE • edited Aug 21, 2019 by JRE

User contributions licensed under CC BY-SA 3.0