2

(**) I created an ASP.NET web server, hosting SignalR hub. I followed this tutorial. It works. Then I created a Xamarin.Android client(*). The problem is I can't connected to the server. I think, I can't connected because of "localhost" in my host address. The exception is pretty big.

If my suspicions are correct, how can I work around? How can I make localhost looks like normal server?

(*) - I also tried with Xamarin.Forms in general.

(**) - Please, look at the "EDIT" at the bottom of the question.

This is my method:

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

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

        // Get our button from the layout resource,
        // and attach an event to it
        Button button = FindViewById<Button>(Resource.Id.MyButton);

        button.Click += async delegate 
        {
            button.Text = string.Format("{0} clicks!", count++);
            var hubConnection = new HubConnection("http://localhost:64642");
            var chatHubProxy = hubConnection.CreateHubProxy("ChatHub");
            try
            {
                // Start the connection
                await hubConnection.Start();

                // Invoke the 'UpdateNick' method on the server
                await chatHubProxy.Invoke("Send", new object[] { "TheMessage", "JohnDoe" });
            }
            catch(Exception ex)
            {

            }
        };
    }

{System.Net.WebException: Error: ConnectFailure (Connection refused) ---> System.Net.Sockets.SocketException: Connection refused at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x000cb] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/System/System.Net.Sockets/Socket.cs:1313 at System.Net.WebConnection.Connect (System.Net.HttpWebRequest request) [0x0019b] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/System/System.Net/WebConnection.cs:195 --- End of inner exception stack trace --- at System.Net.HttpWebRequest.EndGetResponse (System.IAsyncResult asyncResult) [0x0005e] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1023 at System.Threading.Tasks.TaskFactory1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func2[T,TResult] endFunction, System.Action1[T] endAction, System.Threading.Tasks.Task1[TResult] promise, System.Boolean requiresSynchronization) [0x00014] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/threading/Tasks/FutureFactory.cs:550

--- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:535 at System.Net.Http.HttpClientHandler+c__async0.MoveNext () [0x003ce] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClientHandler.cs:379

--- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:535 at System.Net.Http.HttpClient+c__async0.MoveNext () [0x000a9] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:276

--- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:113 at AndroidApp.MainActivity+<>c__DisplayClass1_0+<b__0>d.MoveNext () [0x000a5] in C:\Data\Projects\SinglaRSamples\AndroidApp\AndroidApp\MainActivity.cs:36 }

EDIT:

as mason and Jason suggested, I probably have to use the proper IP instead of "localhost". The IP that Android Emulator see the actual localhost. I'm struggling to find that IP, it's somewhere in Android Settings. Any help would be great!

Ish Thomas
  • 2,270
  • 2
  • 27
  • 57
  • Are you actually running SignalR server on your Android device? – mason Feb 07 '17 at 02:23
  • use the IP or FQDN of your server. "localhost" refers back to the Android device itself. – Jason Feb 07 '17 at 02:27
  • @mason what do you mean? I'm not running the server on Android device. I'm running it in ASPNET website, on localhost. Android device is a client. – Ish Thomas Feb 07 '17 at 02:28
  • Well if you're running an ASP.NET website on a server, and your Xamarin app is running on an Android device, then they're not running on the same machine. Localhost means "same machine". Instead of using localhost in the URL, you'd include the IP address or DNS name of your server. As far as your app is concerned, the server is remote, not local. – mason Feb 07 '17 at 02:31
  • @mason You didn't understand me. I have one computer. On this computer I'm running ASP.NET Web project, where I host signalR hub and I'm running an emulator with Xamarin.Android. The same computer. – Ish Thomas Feb 07 '17 at 02:59
  • It might be the same physical computer, but the app is running on Android emulator inside a Virtual machine, right? So again, you need to provide the correct address to get to the host machine where the sever is actually running. Check the network settings for your VM. – mason Feb 07 '17 at 03:02
  • @Jason I'm on VMWare vm (the computer is Macbook), I'm running a vm with windows. On this VM I'm running everything. Are you suggestion to replace "localhost" with my IP address, like `192.168.123.123:64642/` ? If so, it doesn't work.. I have an error in the browser: `HTTP Error 400. The request hostname is invalid.` (when I go to localhost:64642 - I have a sample chat app, from the tutorial) – Ish Thomas Feb 07 '17 at 03:04
  • The principle is still the exact same. If your ASP.NET server is running on a Windows VM, and you are running the Android emulator VM within that VM, then you need to go into the Android emulator VM's network settings and determine what the correct address is that should be used to refer to the host (Windows VM). This [this question](http://stackoverflow.com/questions/34779282/how-to-connect-to-localhost-from-vs-android-emulator) is similar. You should take a few minutes to learn about virtualization and how virtual networks are set up - it seems like you're confused on the basics. – mason Feb 07 '17 at 03:07
  • ohh, that would make sense. Although, can't find those settings. I'm using the new emulator (HAXM). I probably need to disable LTE, correct? – Ish Thomas Feb 07 '17 at 03:23
  • I doubt LTE has much to do with it. I suggest checking documentation or asking Intel how to check that setting. Or using a different emulator with better documentation. – mason Feb 07 '17 at 03:31
  • @Jason Do you know how to get this IP from Android Emulator settings? I'm struggling to find it :/ – Ish Thomas Feb 07 '17 at 04:01
  • Possible duplicate of [Accessing localhost(Xamarin)](http://stackoverflow.com/questions/40240185/accessing-localhostxamarin) – xleon Feb 07 '17 at 19:17

1 Answers1

6

You can access the ASP.NET site running on your PC host using http://10.0.2.2:64642 from the Google SDK emulator. If it fails to reach the server then you probably need to configure the Windows Firewall to allow incoming HTTP/HTTPS connections.

Trevor Balcom
  • 3,766
  • 2
  • 32
  • 51