2
votes

(**) 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!

1
Are you actually running SignalR server on your Android device?mason
use the IP or FQDN of your server. "localhost" refers back to the Android device itself.Jason
@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
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
@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

1 Answers

6
votes

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.