B4J Question B4J How to ping?

coyote

Member
Licensed User
Longtime User
Hello again,

I want to send a ping to a server but I don't know how to use the RunMethod for Java Objects.
I've found some result, but just for B4A and there the "ping" is not working properly.

B4X:
Sub ping(server as String)
    Dim jo As JavaObject
    jo.InitializeStatic("java.net.InetAddress")
    Dim ping = jo.RunMethod(???) As String

End Sub

I know the keywords, but I don't know how to use them? (InetAddress.IsReachable
/getByAddress)

Coyote

P.S.: Is there a tutorial available for using java objects (with some real examples)?
I like Rob's Code Database but this should be here on this server.
 

giga

Well-Known Member
Licensed User
Longtime User
You could use JShell to do a ping, It will accept the address and number of pings from a textbox and dos shell will appear.

B4X:
Sub btnPing_Action 'Opens Dos box and does ping of the content in the text entered in pinglab and number of time you want to ping in timePing
shl.Initialize("shl", "cmd.exe", ArrayAsString("/c", "start","ping.exe",pinglab.Text, "-n",timePing.Text))
shl.Run(-1)
End Sub

Hope that helps.
;)
 
Upvote 0

coyote

Member
Licensed User
Longtime User
hmm, actually I want to use java objects, because I don't want see the dos box and it works on "all" platforms. (So I think)
I don't need the answer time, reachable or not would be ok.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
quick example - not thoroughly tested though
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private inetaddress As JavaObject
    Private singleaddress As JavaObject

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    singleaddress.InitializeStatic("java.net.InetAddress")
    inetaddress.InitializeStatic("java.net.InetAddress")
    singleaddress = inetaddress.RunMethod("getByName",Array As String("www.hjhkahkd.co.uk"))
    Log(singleaddress)
    If singleaddress.RunMethod("isReachable",Array As Object(2000)) Then
        Log (singleaddress & " is reachable")
    Else
        Log(singleaddress & " is not reachable")
    End If
End Sub
or the terse version
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private inetaddress As JavaObject
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    inetaddress.InitializeStatic("java.net.InetAddress")
    If inetaddress.RunMethodJO("getByName",Array As String("www.hjhkahkd.co.uk")).RunMethodJO("isReachable",Array As Object(2000)) Then
        Log("reachable")
    Else
        Log("unreachable")
    End If
End Sub
 
Last edited:
Upvote 0

coyote

Member
Licensed User
Longtime User
Thanks for reply.
Your first Code works, but only in intranet (my router is reachable).
When I check google.com it's not reachable.
Do you have any ideas why? Is it a DNS problem in Java?
How do I have to write the runmethod when I want to Check a IP Address?


B4X:
Program started.
(Inet4Address) google.com/173.194.70.100
(Inet4Address) google.com/173.194.70.100 is not reachable

Dos Box:
B4X:
c:\ping google.com

Ping wird ausgeführt für google.com [173.194.70.100] mit 32 Bytes Daten:
Antwort von 173.194.70.100: Bytes=32 Zeit=23ms TTL=50
Antwort von 173.194.70.100: Bytes=32 Zeit=23ms TTL=50

Ping-Statistik für 173.194.70.100:
    Pakete: Gesendet = 2, Empfangen = 2, Verloren = 0
    (0% Verlust),
Ca. Zeitangaben in Millisek.:
    Minimum = 23ms, Maximum = 23ms, Mittelwert = 23ms


Your second Code throws this Error (Cannot parse: (Boolean) ):
B4X:
Program started.
Error occurred on line: 18 (main).
java.lang.RuntimeException: Cannot parse: (Boolean) true as boolean
    at anywheresoftware.b4a.BA.parseBoolean(BA.java:270)
    at anywheresoftware.b4a.BA.ObjectToBoolean(BA.java:339)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:469)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:209)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:153)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:69)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
    at b4j.example.main.start(main.java:33)
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:219)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:182)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:179)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
    at java.lang.Thread.run(Thread.java:744)

Coyote
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This line:
B4X:
If inetaddress.RunMethodJO("getByName",Array As String("www.hjhkahkd.co.uk")).RunMethodJO("isReachable",ArrayAsObject(2000)) Then
Should be changed to:
B4X:
If inetaddress.RunMethodJO("getByName",Array As String("www.hjhkahkd.co.uk")).RunMethod("isReachable",Array As Object(2000)) Then
Try to set the hostname to hjhkahkd.co.uk (without www).
 
Upvote 0

Pascual Pérez

Member
Licensed User
Longtime User
i think that:

B4X:
If inetaddress.RunMethodJO("getByName",ArrayAsString("www.hjhkahkd.co.uk")).RunMethodJO("isReachable",ArrayAsObject(2000)) Then
should be:
B4X:
If inetaddress.RunMethodJO("getByName",ArrayAsString("www.hjhkahkd.co.uk")).RunMethodJO("isReachable",ArrayAsObject(2000)).RunMethod("booleanValue", null) Then
to match the Java return type. Thank you for this example anyway :)
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
As an aside - isReachable() is very unreliable function (as reported on various Java sites) and can return false results.

An alternative suggested is to try and open an http link to the site , if it succeeds then address is reachable.
 
Upvote 0

Lakhtin_V

Active Member
Licensed User
Longtime User
quick example - not thoroughly tested though
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private inetaddress As JavaObject
    Private singleaddress As JavaObject

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    singleaddress.InitializeStatic("java.net.InetAddress")
    inetaddress.InitializeStatic("java.net.InetAddress")
    singleaddress = inetaddress.RunMethod("getByName",Array As String("www.hjhkahkd.co.uk"))
    Log(singleaddress)
    If singleaddress.RunMethod("isReachable",Array As Object(2000)) Then
        Log (singleaddress & " is reachable")
    Else
        Log(singleaddress & " is not reachable")
    End If
End Sub
or the terse version
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private inetaddress As JavaObject
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    inetaddress.InitializeStatic("java.net.InetAddress")
    If inetaddress.RunMethodJO("getByName",Array As String("www.hjhkahkd.co.uk")).RunMethodJO("isReachable",Array As Object(2000)) Then
        Log("reachable")
    Else
        Log("unreachable")
    End If
End Sub
I'm using B4A and I want to know the time it took for a signal to reach a specific server. How can I use this library for this or maybe you know other ways to get the ping time.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I'm using B4A and I want to know
It is a mistake to post in a 8 years old thread.
You should always create a new thread for your Issues.
 
Upvote 0
Top