B4J Question Get IP address from WIFI not from ETHERNET

petr4ppc

Active Member
Licensed User
Dear friends,

please for help,

I have PC with ETHERNET connection and WIFI connection

I have in code:
B4X:
Dim server1 As ServerSocket      
Dim deviceID As String

server1.Initialize(5505, "server1")        
deviceID = server1.GetMyIP
ipadressofmycomputer = server1.GetMyIP
and I get IP address from "ethernet 3" but I need IP address from "WIFI"

I can not disable ETHERNET 3.

How can I get IP address form WIFI, please?

Best regards
p4ppc
 
Last edited:

petr4ppc

Active Member
Licensed User
Dear MathlasM

I not understand...

You mean "ipconfig" via cmd?

I need get correct IP address of my pc from WIFI - then I want send UDP data between this PC and for example tablet....
I can do this, but I can not get right IP address of PC. I get IP of Ethernet, but I need IP from Wifi adapter.


Best regards
p4ppc
 
Last edited:

petr4ppc

Active Member
Licensed User
Dear Erel,

Thank you for your answer
I am trying in B4J this:

B4X:
Dim jo As JavaObject
  jo.InitializeContext
  jo.RunMethod("PrintAllInterfaces", Null)
 
#if JAVA
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;

public static void PrintAllInterfaces() throws Exception {
  for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
     BA.Log("" + e.nextElement());
  }
  }
 
#End If
and I get this in Log:

java.lang.NoSuchFieldException: sharedProcessBA
at java.lang.Class.getDeclaredField(Class.java:2070)
at anywheresoftware.b4j.object.JavaObject.InitializeContext(JavaObject.java:55)
at MM.example.main._appstart(main.java:919)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at MM.example.main.start(main.java:41)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Best regards,
p4ppc
 

mark35at

Well-Known Member
Licensed User
Have you tried post #4 in the link Erel has provided above? It gives the address of wlan0.
 

petr4ppc

Active Member
Licensed User
Dear mark35at,

yes I have tried the link from #4 (its from B4A forum):
https://www.b4x.com/android/forum/threads/serversocket-getmyip-returns-wrong-ip.60071/#post-378724
I have tried it in B4J project and this is the result :

java.lang.NoSuchFieldException: sharedProcessBA
at java.lang.Class.getDeclaredField(Class.java:2070)
at anywheresoftware.b4j.object.JavaObject.InitializeContext(JavaObject.java:55)
at MM.example.main._appstart(main.java:919)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at MM.example.main.start(main.java:41)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)

this is my B4J code:

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
 
  Dim jo As JavaObject
  jo.InitializeContext
  jo.RunMethod("PrintAllInterfaces", Null)
   
End Sub

#if JAVA
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;

public static void PrintAllInterfaces() throws Exception {
  for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
     BA.Log("" + e.nextElement());
  }
  }
 
#End If
Best regards
p4ppc
 
Last edited:

petr4ppc

Active Member
Licensed User
Dear THETAHSK

It looks good, but I get result without SSID names and without IP adresses:


name:lo (Software Loopback Interface 1)
name:net0 (WAN Miniport (SSTP))
name:eth0 (WAN Miniport (IPv6))
name:eth1 (WAN Miniport (Network Monitor))
name:eth2 (WAN Miniport (IP))
name:eth11 (Motorola USB Networking Driver #4)
name:wlan0 (TP-LINK Wireless USB Adapter)
name:wlan1 (Microsoft Virtual WiFi Miniport Adapter)
name:net13 (Microsoft ISATAP Adapter #2)
name:net14 (Microsoft ISATAP Adapter #2)
name:eth12 (Remote NDIS based Internet Sharing Device)
name:net15 (Microsoft ISATAP Adapter #2)
name:wlan2 (TP-LINK Wireless USB Adapter-Native WiFi Filter Driver-0000)
name:net16 (Microsoft ISATAP Adapter #5)
name:net17 (Microsoft ISATAP Adapter #6)
name:eth15 (WAN Miniport (Network Monitor)-QoS Packet Scheduler-0000)
name:wlan3 (TP-LINK Wireless USB Adapter-QoS Packet Scheduler-0000)
name:wlan4 (TP-LINK Wireless USB Adapter-WFP LightWeight Filter-0000)
name:wlan5 (TP-LINK Wireless USB Adapter-Virtual WiFi Filter Driver-0000)
name:wlan6 (Microsoft Virtual WiFi Miniport Adapter-Native WiFi Filter Driver-0000)
name:net18 (Microsoft ISATAP Adapter #4)
 

petr4ppc

Active Member
Licensed User
THETAHSK

I not see my SSID name in Logs (its: base). I not see IP addresses.

Because I not see this in logs then I dont get result from second snippet as you wrote:
B4X:
Log(jo.RunMethod("getIp", Array("base")))
Here is code, please check it:
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
 
     Dim jo As JavaObject=Me
     jo.RunMethod("PrintAllInterfaces",Null)
     Log(jo.RunMethod("getIp", Array("base"))) '
End Sub

#if JAVA
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;

public static void PrintAllInterfaces() throws Exception {
  for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
     BA.Log("" + e.nextElement());
  }
  }
 
#End If

#if JAVA
import java.net.InetAddress;
import java.net.Inet6Address;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;

public static String getIp(String interfaceName) throws Exception {
  for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
     NetworkInterface ni = e.nextElement();
     if (ni.getName().equals(interfaceName)) {
       Enumeration<InetAddress> en2 = ni.getInetAddresses();
       while (en2.hasMoreElements()) {
         InetAddress ia = en2.nextElement();
         if (!ia.isLoopbackAddress()) {
           if ((ia instanceof Inet6Address) == false) {
             return ia.getHostAddress();
           }
         }
       }
     }
  }
    return "";
  }
 
#End If
My B4J app use(get) IP address from Ethernet, but I need connect my B4J app to Wifi adapter. My B4J app need IP from wifi adapter not ethernet please. This is why I created this thread. Please for help.

Best regards
p4ppc
 
Last edited:

madru

Active Member
Licensed User
how about >netsh wlan show all< in Windows and pars the result

can you really get that info in Java without using JNI?

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show


    Dim jo As JavaObject=Me    
    jo.RunMethod("listIF",Null)
   
End Sub

#if JAVA
import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;

   public static void listIF() throws SocketException {
        Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface netint : Collections.list(nets))
            displayInterfaceInformation(netint);
    }
 
  static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
     BA.Log("Display name: "+netint.getDisplayName());
     BA.Log("Name: "+netint.getName());
        Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
        for (InetAddress inetAddress : Collections.list(inetAddresses)) {
            BA.Log("InetAddress: "+ inetAddress);
        }
     }

#End If
 

petr4ppc

Active Member
Licensed User
Madru (it is functioned) and Thetahsk( yes,I must use name wlan0), thank you very much

I can check now right IP adress. Thank you very much!

What I must do in next step please If I want join my B4J app only to wlan0 adapter?

B4X:
Dim server1 As ServerSocket
server1.Initialize(5505, "server1")
myip= server1.GetMyIP
In "myip" I need get IP from WIFI adapter, not from ethernet. What can I do please?

Here is code for better understanding, please watch - PERFECT and WRONG:

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private Label1 As Label
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.Show
 
Dim jo As JavaObject=Me
Dim rightipfromwlan As String
Log ("CORRECT IP from wlan0 is :" & jo.RunMethod("getIp", Array("wlan0")))
rightipfromwlan=jo.RunMethod("getIp", Array("wlan0"))

Dim server1 As ServerSocket
Dim myip As String
server1.Initialize(5505, "server1")
myip= server1.GetMyIP
Log ("B4J get this IP:" & myip)

If myip=rightipfromwlan Then
        Log ("PERFECT - I have IP from WLAN - program will continue")
      Else
        Log ("WRONG - program get IP from ETHERNET and not from WIFI adapter")
End If


End Sub

#if JAVA
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;

public static void PrintAllInterfaces() throws Exception {
  for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
     BA.Log("" + e.nextElement());
  }
  }
 
#End If

#if JAVA
import java.net.InetAddress;
import java.net.Inet6Address;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;

public static String getIp(String interfaceName) throws Exception {
  for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
     NetworkInterface ni = e.nextElement();
     if (ni.getName().equals(interfaceName)) {
       Enumeration<InetAddress> en2 = ni.getInetAddresses();
       while (en2.hasMoreElements()) {
         InetAddress ia = en2.nextElement();
         if (!ia.isLoopbackAddress()) {
           if ((ia instanceof Inet6Address) == false) {
             return ia.getHostAddress();
           }
         }
       }
     }
  }
    return "";
  }
 
#End If

#if JAVA
import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;

   public static void listIF() throws SocketException {
        Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface netint : Collections.list(nets))
            displayInterfaceInformation(netint);
    }
 
  static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
     BA.Log("Display name: "+netint.getDisplayName());
     BA.Log("Name: "+netint.getName());
        Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
        for (InetAddress inetAddress : Collections.list(inetAddresses)) {
            BA.Log("InetAddress: "+ inetAddress);
        }
     }

#End If
I found this:
https://www.b4x.com/android/forum/threads/ip-address.53802/#post-337855

Thank you very much,
Best regards
p4ppc
 
Last edited:
Top