B4J Question Ip Address

Discussion in 'B4J Questions' started by galeforce9, May 15, 2015.

  1. galeforce9

    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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    ServerSockets is bound to all the network interfaces. Are you able to connect from the client with the correct ip address?
     
  3. galeforce9

    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
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    I've just rechecked the ServerSocket code. The socket is bound to all interfaces (this is the default behavior if you don't explicitly set a local ip address).
     
  5. galeforce9

    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
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    ServerSocket is based on Java ServerSocket. Try to find Java code that is supposed to solve this issue and I will try to help you port it.
     
  7. galeforce9

    galeforce9 Member Licensed User

    Hello Erel,

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

    Code:
    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
     
  8. Erel

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

    Attached Files:

    Last edited: Nov 6, 2015
  9. galeforce9

    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
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    Have you created a non-UI app? If not then you don't need to call it.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice