B4A Library [B4X] SP Ping - Run the ping command B4A, B4J and B4I

SP Ping allows you to run the ping command on B4A, B4J and B4I. The characteristics of each operating system require different external libraries and change the amount of information returned after execution. In red we show you the fields that are empty in each operating system.

  • Ping
    • Events
      • PingStarted
        Triggers when the ping starts
      • PingFinished
        Triggers when ping stops
      • PingError (Msg As String)
      • Trigger when an error occurs, they can be the following:
        • Attempts must be greater than or equal to 1
        • Host unreachable
        • ERROR: LANGUAGE NOT FOUND (Only B4J)
        • Can't run ping command on your system (Only B4J)
    • Types
      • tpePing (Success As Boolean, toHost As String, toIP As String, fromDest As String, mapResponse As Map, mapStatistics As Map)
        • IsConnected: True or False
        • toHost: This value can be a URL or an IP
        • toIP: The local IP address that DNS resolves to the URL mapped in toHost (if toHost is an IP address, repeat the same value)
        • fromDest: The IP address or hostname that responds to the Ping command
        • mapResponse: Map variable with a collection of Ping responses (details below)
        • mapStatistics: Map type variable with a collection of Ping command statistics (details below)
    • Fields
      • Tag As Object
        • Default: Null (It is always valuable to have a Tag field ;))
      • pingResult As tpePing
        • Default: Success = False, toHost = "", toIP = "", fromDest = "", mapResponse = Null, mapStatistics = Null
      • Destination As String
        • Default: "google.com"
      • Attempts As Byte
        • Default = 1
      • Timeout As Int
        • Default = 200
    • Methods
      • Initialize (CallBack As Object, EventName As String)
        Initializes the object. Add parameters to this method.
      • Start
        Start the ping command on your system
Change log:
  • 1.00
    • Release
B4XMainPage:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private spPing As Ping
    Private Button1 As Button
End Sub

Public Sub Initialize
    B4XPages.GetManager.LogEvents = True
    spPing.Initialize(Me, "SPPing")
    spPing.Destination = "google.com"
    spPing.Attempts = 4
    spPing.Timeout = 200
End Sub

Private Sub SPPing_PingStarted
    Log("Ping started")
End Sub

Private Sub SPPing_PingFinished
    If spPing.pingResult.Success Then
        Log("Success: " & spPing.pingResult.Success)
        Log("To host: " & spPing.pingResult.toHost)
        Log("To IP: " & spPing.pingResult.toIP)
        Log("From host: " & spPing.pingResult.fromDest)
        For Each bteKey As Byte In spPing.pingResult.mapResponse.Keys
            Dim seqMap As Map = spPing.pingResult.mapResponse.Get(bteKey)
            Log("Sequence " & bteKey & " - TTL : " & seqMap.Get("ttl") & " - Time : " & seqMap.Get("time"))
        Next
        Dim stats As Map = spPing.pingResult.mapStatistics
        Log("Packets transmitted: " & stats.Get("packets transmitted"))
        Log("Packets received: " & stats.Get("received"))
        Log("Packets loss: " & stats.Get("packet loss"))
        Log("Round Trip Time (Minimum): " & stats.Get("rtt min"))
        Log("Round Trip Time (Average): " & stats.Get("rtt agv"))
        Log("Round Trip Time (Maximum): " & stats.Get("rtt max"))
        Log("Round Trip Time (Mean Deviation): " & stats.Get("rtt mdev"))
    End If
End Sub

Private Sub SPPing_PingError(Msg As String)
    Log(Msg)
End Sub

Private Sub Button1_Click
    spPing.Start
End Sub

B4A
Additional libraries (not internal):
Does not depend on additional libraries
Additional files: None
Information returned in this environment:
Success: true
To host: google.com
To IP: 216.58.202.78
From host: gru10s11-in-f14.1e100.net
Sequence 1 - TTL : 116 - Time : 24.0
Sequence 2 - TTL : 116 - Time : 24.0
Sequence 3 - TTL : 116 - Time : 24.0
Sequence 4 - TTL : 116 - Time : 24.0
Packets transmitted: 4
Packets received: 4
Packets loss: 0
Round Trip Time (Minimum): 22.321
Round Trip Time (Average): 25.255
Round Trip Time (Maximum): 29.231
Round Trip Time (Mean Deviation): 2.549

B4J
Additional libraries (not internal):
Attached B4JPing by @Mariano Ismael Castro (includes the java source code)
Additional files: It depends on this json file that contains the ping responses in different languages. If you can't find your language, follow the instructions in the post.
Information returned in this environment:
Success: true
To host: google.com
To IP: 172.217.173.238
From host: 172.217.173.238
Sequence 1 - TTL : 116 - Time : 18
Sequence 2 - TTL : 116 - Time : 18
Sequence 3 - TTL : 116 - Time : 18
Sequence 4 - TTL : 116 - Time : 18
Packets transmitted: 4
Packets received: 4
Packets loss: 0
Round Trip Time (Minimum): 17
Round Trip Time (Average): 20
Round Trip Time (Maximum): 18
Round Trip Time (Mean Deviation): null

B4I
Additional libraries (not internal):
@Erel's iPing library
Information returned in this environment:
Success: true
To host: google.com
To IP:
From host:
Sequence 1 - TTL : null - Time : 44
Sequence 2 - TTL : null - Time : 44
Sequence 3 - TTL : null - Time : 44
Sequence 4 - TTL : null - Time : 44
Packets transmitted: null
Packets received: null
Packets loss: null
Round Trip Time (Minimum): null
Round Trip Time (Average): null
Round Trip Time (Maximum): null
Round Trip Time (Mean Deviation): null

The result in the log
1719585265045.png


I want to thank the users who helped create this library for B4X.

Thank you, @Erel, for your infinite patience with questions that many have surely asked you hundreds of times, and a very special thanks to @Mariano Ismael Castro for sharing your knowledge in Java and not only sharing the library, also the source code available.
 

Attachments

  • B4JPing.zip
    5.6 KB · Views: 15
  • Ping.b4xlib
    2.7 KB · Views: 18
Last edited:
Top