Bug 6308 - Runtime error while using Java.Net.InetAddress or System.Net.NetworkInformation.Ping to ping a server
Summary: Runtime error while using Java.Net.InetAddress or System.Net.NetworkInformati...
Status: RESOLVED INVALID
Alias: None
Product: Xamarin Studio
Classification: Desktop
Component: Android Add-in ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2012-07-27 11:56 UTC by tej mannava
Modified: 2012-07-27 17:50 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED INVALID

Description tej mannava 2012-07-27 11:56:51 UTC
I am trying to ping a server and check if the server is available.
In the example below I mentioned "google.com" but in our application I am calling our app server

The call to InetAddress.GetByName("servername") is giving a runtime error.

//using Java.net

String hostname = "google.com";
InetAddress addr = InetAddress.GetByName(hostname);


As an alternative I tried using .Net libraries and it gave similar runtime error

//using System.Net
//using System.Net.NetworkInformation

String hostname = "google.com";
Ping ping = new Ping();
PingReply reply = ping.Send(hostname, 3);
Comment 1 Jonathan Pryor 2012-07-27 14:05:19 UTC
What is the runtime error? Please provide the full stack trace as obtained from: http://docs.xamarin.com/android/advanced_topics/android_debug_log

InetAddress.getByName() is documented as possibly throwing an UnknownHostException:

http://developer.android.com/reference/java/net/InetAddress.html#getByName(java.lang.String)

Ping.Send() is also documented as throwing several exceptions:

http://msdn.microsoft.com/en-us/library/dcc2xffs.aspx

As a final sanity check, are you able to access your site from the Browser app on the device?
Comment 2 tej mannava 2012-07-27 17:05:28 UTC
Yes, I am able to call the services hosted on the server from the app.
When I try to have a check if the server is available before the actual service call it is causing this problem.
Please see below the stack traces for both "InetAddress" and "Ping" calls

InetAddress.getByName()   Stack Trace

at Java.Net.InetAddress.IsReachable (int) [0x00067] 
in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Java.Net.InetAddress.cs:538
at NOCSiteCheckin.ServerConnection.GetServerInfo () [0x0001a] 
in c:\Tej\Metro\NOCSiteCheckin\NOCSiteCheckin\Resources\ServerConnection.cs:25
at NOCSiteCheckin.LoginActivity..ctor () [0x00000] 
in c:\Tej\Metro\NOCSiteCheckin\NOCSiteCheckin\LoginActivity.cs:18
at (wrapper dynamic-method) object.a0ca80f1-4886-49b4-97f8-8be534e00402 (intptr,object[]) <IL 0x00018, 0x00053>
at Java.Interop.TypeManager.n_Activate (intptr,intptr,intptr,intptr,intptr,intptr) [0x00069] 
in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/monodroid/src/Mono.Android/src/Java.Interop/TypeManager.cs:141
at (wrapper native-to-managed) Java.Interop.TypeManager.n_Activate (intptr,intptr,intptr,intptr,intptr,intptr) <IL 0x0001e, 0x000a3>


Ping.Send()  Stack Trace

System.InvalidOperationException: Process has not been started.
  at System.Diagnostics.Process.get_HasExited () [0x00015] 
  in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/mono/mcs/class/System/System.Diagnostics/Process.cs:195

  at (wrapper remoting-invoke-with-check) System.Diagnostics.Process.get_HasExited () <IL 0x00032, 0x0007f>

  at System.Net.NetworkInformation.Ping.SendUnprivileged (System.Net.IPAddress,int,byte[],System.Net.NetworkInformation.PingOptions) [0x00116] 
  in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/mono/mcs/class/System/System.Net.NetworkInformation/Ping.cs:315

  at System.Net.NetworkInformation.Ping.Send (System.Net.IPAddress,int,byte[],System.Net.NetworkInformation.PingOptions) [0x00067] 
  in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/mono/mcs/class/System/System.Net.NetworkInformation/Ping.cs:213

  at System.Net.NetworkInformation.Ping.Send (string,int,byte[],System.Net.NetworkInformation.PingOptions) [0x00007] 
  in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/mono/mcs/class/System/System.Net.NetworkInformation/Ping.cs:188

  at System.Net.NetworkInformation.Ping.Send (string,int,byte[]) [0x00000] 
  in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/mono/mcs/class/System/System.Net.NetworkInformation/Ping.cs:182

  at System.Net.NetworkInformation.Ping.Send (string,int) [0x00000] 
  in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/mono/mcs/class/System/System.Net.NetworkInformation/Ping.cs:177

  at (wrapper remoting-invoke-with-check) System.Net.NetworkInformation.Ping.Send (string,int) <IL 0x00034, 0x000bf>

  at NOCSiteCheckin.ServerConnection.GetServerInfo () [0x00019] 
  in c:\Tej\Metro\NOCSiteCheckin\NOCSiteCheckin\Resources\ServerConnection.cs:27

  at NOCSiteCheckin.LoginActivity..ctor () [0x00000] 
  in c:\Tej\Metro\NOCSiteCheckin\NOCSiteCheckin\LoginActivity.cs:18

  at (wrapper dynamic-method) object.84a16b5f-1f63-43bd-b322-b4b1217bc941 (intptr,object[]) <IL 0x00018, 0x00053>

  at Java.Interop.TypeManager.n_Activate (intptr,intptr,intptr,intptr,intptr,intptr) [0x00069] 
  in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/monodroid/src/Mono.Android/src/Java.Interop/TypeManager.cs:141

  at (wrapper native-to-managed) Java.Interop.TypeManager.n_Activate (intptr,intptr,intptr,intptr,intptr,intptr) <IL 0x0001e, 0x000a3>
Comment 3 Jonathan Pryor 2012-07-27 17:27:20 UTC
> InetAddress.getByName()   Stack Trace
> at Java.Net.InetAddress.IsReachable (int) [0x00067] 

This stack trace is incomplete; they should contain the type thrown, and the underlying Java stack trace. For example, this C# fragment:

	throw new Java.Lang.AbstractMethodError ("Boo!");

produces:

> Unhandled Exception: Java.Lang.AbstractMethodError: Exception of type 'Java.Lang.AbstractMethodError' was thrown.
> at Bxc6308.Ping.Activity1.OnCreate (Android.OS.Bundle) <0x000fc>
> at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) <0x00057>
> at (wrapper dynamic-method) object.a6eb54ff-3352-448f-b56e-704ff0511069 (intptr,intptr,intptr) <0x00033>
> 
>   --- End of managed exception stack trace ---
> java.lang.AbstractMethodError: Boo!
> 	at bxc6308.ping.Activity1.n_onCreate(Native Method)
> 	at bxc6308.ping.Activity1.onCreate(Activity1.java:28)
> 	at android.app.Activity.performCreate(Activity.java:5008)
> 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
> 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
> 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
> 	at android.app.ActivityThread.access$600(ActivityThread.java:130)
> 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
> 	at android.os.Handler

Notice that we have the type thrown (AbstractMethodError), and a "End of managed exception stack trace" section.

Regarding the exception in Ping.Send(), I am able to reproduce that.
Comment 4 tej mannava 2012-07-27 17:35:40 UTC
Here is the complete stack trace for InetAddress


Java.Lang.RuntimeException: 
  at Android.Runtime.JNIEnv.CallNonvirtualBooleanMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) [0x00025] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:417

  at Java.Net.InetAddress.IsReachable (int) [0x00067] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Java.Net.InetAddress.cs:538

  at NOCSiteCheckin.ServerConnection.GetServerInfo () [0x0001a] in c:\Tej\Metro\NOCSiteCheckin\NOCSiteCheckin\Resources\ServerConnection.cs:26

  at NOCSiteCheckin.LoginActivity..ctor () [0x00000] in c:\Tej\Metro\NOCSiteCheckin\NOCSiteCheckin\LoginActivity.cs:18

  at (wrapper dynamic-method) object.68db60f2-4618-400d-af9b-3404e3bcd31c (intptr,object[]) <IL 0x00018, 0x00053>

  at Java.Interop.TypeManager.n_Activate (intptr,intptr,intptr,intptr,intptr,intptr) [0x00069] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/monodroid/src/Mono.Android/src/Java.Interop/TypeManager.cs:141

  at (wrapper native-to-managed) Java.Interop.TypeManager.n_Activate (intptr,intptr,intptr,intptr,intptr,intptr) <IL 0x0001e, 0x000a3>

  

  --- End of managed exception stack trace ---

  android.os.NetworkOnMainThreadException

  	at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)

  	at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:74)

  	at libcore.io.IoBridge.connectErrno(IoBridge.java:138)

  	at libcore.io.IoBridge.connect(IoBridge.java:112)

  	at java.net.InetAddress.isReachable(InetAddress.java:730)

  	at java.net.InetAddress.isReachable(InetAddress.java:684)

  	at java.net.InetAddress.isReachable(InetAddress.java:654)

  	at mono.android.TypeManager.n_activate(Native Method)

  	at mono.android.TypeManager.Activate(TypeManager.java:7)

  	at nocsitecheckin.LoginActivity.<init>(LoginActivity.java:22)

  	at java.lang.Class.newInstanceImpl(Native Method)

  	at java.lang.Class.newInstance(Class.java:1319)

  	at android.app.Instrumentation.newActivity(Instrumentation.java:1023)

  	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)

  	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)

  	at android.app.ActivityThread.access$600(ActivityThread.java:122)

  	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)

  	at android.os.Handler.dispatchMessage(Handler.java:99)

  	at android.os.Looper.loop(Looper.java:137)

  	at android.app.ActivityThread.main(ActivityThread.java:4340)

  	at java.lang.reflect.Method.invokeNative(Native Method)

  	at java.lang.reflect.Method.invoke(Method.java:511)

  	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

  	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

  	at dalvik.system.NativeStart.main(Native Method)
Comment 5 Jonathan Pryor 2012-07-27 17:47:16 UTC
The Ping.Send() exception has been moved to #6312.
Comment 6 Jonathan Pryor 2012-07-27 17:50:32 UTC
Ah, this is very important:

> Java.Lang.RuntimeException: 
...
>  android.os.NetworkOnMainThreadException

Honeycomb no longer allows network operations to be performed from the Main (UI) thread:

http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception

The fix is to either move the network call to a worker thread, or tell Android to stuff it:

https://github.com/xamarin/monodroid-samples/blob/b8326c74/SanityTests/Hello.cs#L285