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:
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

Issues to resolve:
- Is it possible that cmd needs special permissions?
- Special characters (accents and others) -> Pinger.Encoding = "Encoding Value"
 
Last edited:

Sergio Haurat

Active Member
Licensed User
Longtime User
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:
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

Sergio Haurat

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

 
Last edited:
Upvote 0

Mariano Ismael Castro

Active Member
Licensed User

Correct code java
getIPFromHost:
 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;
    }


Get IP From Host:
Dim IP As String = ping.getIPFromHost("www.google.com")   
Log(IP) '142.251.35.228
 
Upvote 0

Sergio Haurat

Active Member
Licensed User
Longtime User
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

Mariano Ismael Castro

Active Member
Licensed User
This works between #IF JAVA without needing to import anything or is it the correct way for my imaginary class?

B4XMainPage:
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
Cookies are required to use this site. You must accept them to continue using the site. Learn more…