B4J Question Ip Address

galeforce9

Member
Licensed User
Hi

I've been trying to get a socket example to work between a PC running b4j and an android device running b4a. I have not been having any success but on the last example (cctv example) it displays the server IP address which was the incorrect one. My dev machine I have to run usually four different Ip addresses so I can communicate with the different equipment on a customers site. However server.getmyip returns only one of the addresses (actually the second one!). How can I get a list of the IP addresses and then bind the socket to the correct one.

Thank you

Ian
 

galeforce9

Member
Licensed User
Hi Erel,

No I could not get any of the socket examples to work. After seeing the incorrect address showing on the cctv example (pc had all sub nets and android had just one of them) I deleted all the extra sub nets from the PC leaving just the one the android device had. Everything started working straight away on the examples.

Hence my question as it appears on the PC side it did only bind to one address not all the ip address set on the same interface.

Thank you

Ian
 

galeforce9

Member
Licensed User
Hi

It may be bound to all interfaces but it is definitely bound to all IP addresses on a single interface. As I mentioned a Single interface with multiple IP addresses would not allow a connection on one of the subnets as soon as a made just a single address on the interface it worked straight away with not other changes. You should be able to see the effect if you add an extra ip address range / subnet onto an interface. I'm not sure how else I could demonstrate this. For reference my dev PC is a windows 7 machine running B4J.

Thanks

Ian
 

galeforce9

Member
Licensed User
Hello Erel,

I dont know if this helps or not but from my little knowledge it seemed about right:

B4X:
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;

public class Main {
  static public void main(String args[]) throws Exception {
    int port = 80;

    NetworkInterface ni = NetworkInterface.getByName("name");
    Enumeration e = ni.getInetAddresses();
    if (!e.hasMoreElements())
      return;
    InetAddress ia = (InetAddress) e.nextElement();

    ServerSocket ss = new ServerSocket(port, 20, ia);
    System.out.println("Listening");
    Socket s = ss.accept();
    System.out.println(s);
  }
}
If I read the code correctly it does use "getinetaddresses" but then loops by checking NOT e.hasmorelements for further addresses. I believe it then creates a server socket on the same port for all available IP addresses. Again please forgive me if this is not what you were after.

Thank you

Ian
 

Erel

Administrator
Staff member
Licensed User
Copy the attached library to the internal libraries folder.

You can now use this code to print all interfaces and select the one that the server socket will bind to:
B4X:
Sub Process_Globals
   Private servSocket As ServerSocket  
End Sub

Sub AppStart (Args() As String)
   servSocket.Initialize(0, "servSocket")
   Dim jo As JavaObject = Me
   jo.RunMethod("PrintAllInterfaces", Null)
   jo.RunMethod("InitServerSocket", Array(servSocket, "lo", 5000))
   servSocket.Listen
   StartMessageLoop
End Sub

#if JAVA
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;
import anywheresoftware.b4a.objects.SocketWrapper.ServerSocketWrapper;
public static void PrintAllInterfaces() throws Exception {
      for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
    System.out.println(e.nextElement());
      }
   }
  
public static void InitServerSocket(ServerSocketWrapper ServerSocket, String interfaceName, int port) throws Exception {
     NetworkInterface ni = NetworkInterface.getByName(interfaceName);
    Enumeration e = ni.getInetAddresses();
    InetAddress ia = (InetAddress) e.nextElement();
     ServerSocket.ssocket = new ServerSocket(port, 0, ia);
}
#End If
 

Attachments

Last edited:

galeforce9

Member
Licensed User
Hello Erel,

Thank you for a very quick response. I have added the contents of the zip file into the internal library. I have selected to use the network and java libraries. When I try to compile I get

Error description: Undeclared variable 'startmessageloop' is used before it was assigned any value.

Thank you

Ian
 
Top