B4J Question Shell running ping

Sergio Haurat

Active Member
Licensed User
Longtime User
I have this code that just tries to ping using shell with the following command line.
B4X: Copy
Sub Class_Globals
  Private Pinger As Shell
  Public Destination As String
  Public Attempts As Byte
  Public Timeout As Int
End Sub

Public Sub Initialize(CallBack As Object, EventName As String)
  Destination = "google.com"
  Attempts = 4
  Timeout = 200
End Sub

Private Sub DesktopDoPing()
  If Attempts = 0 Then
    If xui.SubExists(mCallback, mEventName & "_PingError", 1) Then
       CallSub2(mCallback, mEventName & "_PingError", "Attempts must be greater than or equal to 1")
    End If
  Else
    If xui.SubExists(mCallback, mEventName & "_PingStarted", 0) Then
      CallSub(mCallback, mEventName & "_PingStarted")
    End If
    'Pinger.Initialize("Desktop", "ping", Array(Destination, " -w " & Timeout, " -n " & Attempts))
    Pinger.Initialize("Desktop", "cmd.exe", Array("/c ", " ping ", Destination, " -w " & Timeout, " -n " & Attempts))
    If Pinger.IsInitialized Then
      'The execution has a lifespan calculated as "response time * attempts" + 5 seconds.
      Pinger.Run((Timeout * Attempts) + 5000)
    End If
  End If
End Sub

Private Sub Desktop_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
  Log(StdOut)
End Sub

LOG lines
WARNING: package com.sun.javafx.embed.swing.oldimpl not in javafx.swing
Waiting for debugger to connect...
Program started.
*** mainpage: B4XPage_Created
Ping started
*** mainpage: B4XPage_Appear
*** mainpage: B4XPage_Resize [mainpage]
~l041179649:
Haciendo ping a google.com [142.251.133.14] con 32 bytes de datos:
PING: error en la transmisi�n. Error general.
PING: error en la transmisi�n. Error general.
PING: error en la transmisi�n. Error general.
PING: error en la transmisi�n. Error general.
~l041179649:
Estad�sticas de ping para 142.251.133.14:
Paquetes: enviados = 4, recibidos = 0, perdidos = 4
(100% perdidos),

Issues to resolve:
- Is it possible that cmd needs special permissions?
- Special characters (accents and others) -> Pinger.Encoding = "Encoding Value"
 
Last edited:
I'm going crazy ? looking at your source code, compatible with B4J... I can think of a very simple function that I don't think exists natively in network, for example, see my IP resolution from a name. Looking at your code I think of something like this and that it works between #IF JAVA/#END IF

I don't know how to program a single line of Java, but I do develop in PHP and C#, from the same family of syntax.

Java: Copy
package com.micr.b4jDNS;

import anywheresoftware.b4a.BA;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class jDNS {
    private BA ba;
    private String eventName;
    private Object callBack;
    private String Host;
    public void Initialize(BA paramBA, Object mCallBack, String EventName) {
        this.ba = paramBA;
        this.eventName = EventName.toLowerCase(BA.cul);
        this.callBack = mCallBack;
    }

    private void ResponseSuccesful(String IP, boolean Success) {
        if (ba.subExists(eventName + "_ResponseSuccesful")) {
            ba.raiseEventFromUI(callBack, eventName + "_ResponseSuccesful", IP, Success);
        }
    }
    
    public String getIPFromHost(String hostName, int timeOut) {
        boolean result = false;
        String IP = "";
        try {
            InetAddress geek = InetAddress.getByName(this.Host);
            result = geek.isReachable(timeOut);
            ResponseSuccesful(geek, true)
        } catch (UnknownHostException ex) {
           anywheresoftware.b4a.BA.LogInfo("Unknown host"); 
           anywheresoftware.b4a.BA.Log(ex.getMessage());

        } catch (IOException ex) {
             anywheresoftware.b4a.BA.Log(ex.getMessage());
        }
        return result;
    }
}
 
Upvote 0
It works great! I just have to find a way for the response to be with the appropriate Encoding or the system default

1719290199944.png
 
Last edited:
Upvote 0
public String getIPFromHost(String hostName, int timeOut) {
boolean result = false;
String IP = "";
try {
InetAddress geek = InetAddress.getByName(this.Host);
result = geek.isReachable(timeOut);
ResponseSuccesful(geek, true)
} catch (UnknownHostException ex) {
anywheresoftware.b4a.BA.LogInfo("Unknown host");
anywheresoftware.b4a.BA.Log(ex.getMessage());

} catch (IOException ex) {
anywheresoftware.b4a.BA.Log(ex.getMessage());
}
return result;
}
}[/CODE]

Correct code java
Code: Copy
 public String getIPFromHost(String hostName) {
        String IP = "";
        try {
            InetAddress geek = InetAddress.getByName(hostName);
            IP = geek.getHostAddress();
        } catch (UnknownHostException ex) {
            anywheresoftware.b4a.BA.LogInfo("Unknown host");
            anywheresoftware.b4a.BA.Log(ex.getMessage());

        } catch (IOException ex) {
            anywheresoftware.b4a.BA.Log(ex.getMessage());
        }
        return IP;
    }


Code: Copy
Dim IP As String = ping.getIPFromHost("www.google.com")   
Log(IP) '142.251.35.228
 
Upvote 0
Hey, maybe what I'm going to share with you isn't that useful anymore, but I'm going to share it with you anyway. check it out

It works great! I just have to find a way for the response to be with the appropriate Encoding or the system default

View attachment 154886
I can't find a way to convert or run with the correct system encoder.

With shell I first sent the system's default encoder, since I didn't know how to consult it I am saving it in jSON. Then I run it to control the accents. Is there a way to first check what the encoder context of the system that is executing the function is so that it returns the StdOut result correctly?
 
Upvote 0
This works between #IF JAVA without needing to import anything or is it the correct way for my imaginary class?

Code: Copy
Private Sub Button1_Click
    Dim j As JavaObject= Me
    Dim IP As String =j.RunMethod("getIPFromHost", Array("www.google.com"))
    Log(IP)
End Sub

#If Java
import java.net.InetAddress;
import java.io.IOException;
import java.net.UnknownHostException;
 public String getIPFromHost(String hostName) {
        String IP = "";
        try {
            InetAddress geek = InetAddress.getByName(hostName);
            IP = geek.getHostAddress();
        } catch (UnknownHostException ex) {
            anywheresoftware.b4a.BA.Log("Unknown host");
            anywheresoftware.b4a.BA.Log(ex.getMessage());

        } catch (IOException ex) {
            anywheresoftware.b4a.BA.Log(ex.getMessage());
        }
        return IP;
    }
#End If
 
Last edited:
Upvote 0
Back
Top