Network Enumeration

DerekTweed

Member
Licensed User
Longtime User
I would like to be able to obtain a list of ALL IP addresses available, and bind a server socket to a particular one of my choice. EG, my phone may have a mobile carrier-assigned IP addr, a VPN IP addr, and a WiFi IP addr. I apologise in advance if this feature already exists - I'm new to B4A, with which I am very impressed! Best £30 I've ever spent!
 

DerekTweed

Member
Licensed User
Longtime User
Thanks Erel.
I'm writing a port-forward App, which will accept an incoming TCP connection from the mobile carrier, and forward it over the WiFi tether. My mobile carrier doesn't allow incoming connections, so I'm using a VPN to overcome this. As I am making this connection from the VPN server PC, I know what the phone's VPN client IP address is, so it's not a problem.
As you explained, ServerSocket listens to all adaptors, and the app works fine.
It's just that I'd like to be able to show the VPN client IP address as part of the app.

Using the following code
B4X:
   Dim Temp As ServerSocket
   Temp.Initialize(65535,"")
   lblInputIP.Text="Input IP = "& Temp.GetMyIP
   Temp.Close

sometimes returns the mobile carrier-assigned IP address, and sometimes the VPN client IP address.
Following your suggestion, I've read the ABWifi documentation, but I'm not sure how this might help.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
ABWifi will not help here. You will need to write a small library for that.

This is the code that enumerates the networks and returns one of them:
B4X:
public String GetMyIP() throws SocketException {
         String lh = "127.0.0.1";
         String res = GetMyWifiIP();
         if (res.equals(lh) == false)
            return res;
         Inet6Address ip6 = null;
         Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
         if (en != null) {
            while (en.hasMoreElements()) {
               NetworkInterface ni = en.nextElement();
               Enumeration<InetAddress> en2 = ni.getInetAddresses();
               while (en2.hasMoreElements()) {
                  InetAddress ia = en2.nextElement();
                  if (!ia.isLoopbackAddress()) {
                     if (ia instanceof Inet6Address) {
                        if (ip6 == null)
                           ip6 = (Inet6Address) ia;
                     }
                     else
                        return ia.getHostAddress();
                  }
               }
            }
         }
         if (ip6 != null)
            return ip6.getHostAddress();
         return lh;

      }

It shouldn't be difficult to modify it to return all available networks.
 

DerekTweed

Member
Licensed User
Longtime User
Thank you, Erel.

I'm now following your Library Creation Tutorial (FirstLib)


I have a problem with 'Generate JavaDoc'



I get this result:
-------------
java.lang.NoClassDefFoundError: BAdoclet (wrong name: BADoclet)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at com.sun.tools.javadoc.DocletInvoker.<init>(DocletInvoker.java:92)
at com.sun.tools.javadoc.Start.setDocletInvoker(Start.java:441)
at com.sun.tools.javadoc.Start.parseAndExecute(Start.java:218)
at com.sun.tools.javadoc.Start.begin(Start.java:167)
at com.sun.tools.javadoc.Main.execute(Main.java:59)
at com.sun.tools.javadoc.Main.main(Main.java:49)
javadoc: error - fatal error

1 error
----------------


Any help would be appreciated

Thanks in advance
 

DerekTweed

Member
Licensed User
Longtime User
Thanks - I'd already done that. I found the problem - I'd specified the name as
'BAdoclet', not 'BADoclet' in the Doclet name field.:sign0161:
It would have been more helpful if that long error list had simply said -- 'can't find the file!'

So now I have a .xml file - but sadly, no .jar file!

Am I missing a step? I notice on your video Tutorial (which is very helpful -thanks for that) that the time-stamp of the generated .xml file is 4.20, but the .jar file has 4.22??
 

DerekTweed

Member
Licensed User
Longtime User
Yes. Earlier today (but after my previous post), I discovered your other Library tutorial, where you mention the missing step(Export). I did that, and now - I have both .jar and .xml files.

Unfortunately, on testing FirstLib, it didn't work for me, (Log shows this error on starting the app) java.lang NoClass DefFound ...... (not sure of exact text, As I've exited B4A), but I'll take a break from this for a few days, and return later

Thank you for your assistance.....
 

DerekTweed

Member
Licensed User
Longtime User
Well, I just couldn't resist having one more go, using the 1.6 Compliance settings, (I have Java SE1.7 installed) and - FirstLib now works! What a star - thanks!

One final thing. I just tried to create another library using the sample code you supplied in the fourth post of this thread. Eclipse objects to this:

B4X:
 String res = GetMyWifiIP();

and suggests that I might have meant this:
B4X:
 String res = GetMyIP();
but that's the name of the method. Will that work?
Sorry for my lack of Java knowledge...that's why I leapt at Baisc4Android!
 

DerekTweed

Member
Licensed User
Longtime User
I've re-written that code, and now I have exactly what I wanted - a list of the names and corresponding IP addresses of all active interfaces on the device.

Thanks to you, I've learned a lot of new stuff this week - which proves that you're never too old to learn!

Best regards, and thanks again

Derek Tweed
 
Top