Can you explain the Functionality of requestRouteToHost() in android?
Asked Answered
S

2

3

In my code I am using requestRouteToHost() method:

Does this routing means changing the WIFI to 3G or vice versa??

My code is not working...

public static boolean isHostAvailable(Context context, String urlString) throws UnknownHostException, MalformedURLException { 
     boolean ret = false; 
     int networkType = ConnectivityManager.TYPE_WIFI; 
     ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     if(cm != null){ 
             NetworkInfo nf = cm.getActiveNetworkInfo(); 
             if(nf != null){ 
                     networkType = nf.getType(); 
             } 
             URL url = new URL(urlString); 
             InetAddress  iAddress = InetAddress.getByName(url.getHost()); 
             ret = cm.requestRouteToHost(networkType, ipToInt(iAddress.getHostAddress())); 
     } 
     return ret; 
}

public static int ipToInt(String addr) {
     String[] addrArray = addr.split("\\.");

     int num = 0;
     for (int i=0;i<addrArray.length;i++) {
         int power = 3-i;

         num += ((Integer.parseInt(addrArray[i])%256 * Math.pow(256,power)));
     }
     return num;
 }

Thanks

Synchrotron answered 3/8, 2011 at 7:51 Comment(0)
M
0

Method requestRouteToHost() does not change wifi to 3G or vice versa!

Official Documentation :

public boolean requestRouteToHost (int networkType, int hostAddress) 

Ensure that a network route exists to deliver traffic to the specified host via the specified network interface. An attempt to add a route that already exists is ignored, but treated as successful.

  • Parameters

    networkType the type of the network over which traffic to the specified host is to be routed

    hostAddress the IP address of the host to which the route is desired

  • Returns

    true on success, false on failure

Marquettamarquette answered 3/8, 2011 at 7:59 Comment(4)
Thanks.. if i have given the IP address of particular website as the host address can i route this??Synchrotron
you can check if there is a route to the website or not (boolean)Marquettamarquette
@SherifelKhatib what do you mean when you say route to the website? What is route to website?Myelencephalon
@blackcrow route to the website is actually in this case just a boolean that explains whether the host is reachable. Consider it a ping.Marquettamarquette
C
29

I think this is an extremely poorly documented method, and while an above comment saying "consider it a ping" might be a reasonable interpretation, I don't think it's correct. The fact that it takes an int as a host address suggests it is a much lower-level method than that, and the comment in the JavaDoc This method requires the caller to hold the permission CHANGE_NETWORK_STATE is another clue, suggesting that this makes a change in the internal routing table of the device. This link provides a better explanation:

requestRouteToHost() doesn't establish connectivity on any network, it only ensures that any traffic for the specified host will be routed via the specified network type (wifi or mobile). Connectivity must already exist on the specified network.

This explanation makes MUCH more sense, considering the permission required. It also appears that it will not work with WiFi. So, it appears what this method is useful for is the following: You wish to ensure that the connection made to a particular host will be made via a SPECIFIC interface and that interface is not WiFi. This might make sense for a long-term, low traffic, battery efficient connection, such as when you wish to keep a socket open to a server and wait for the server to send the occasional message. The mobile data interface would make more sense than WiFi, since you wouldn't need to keep the WiFi radio active the whole time, and the mobile network radio is always on anyway. Incidentally, this is EXACTLY how an iPhone's server "push" mechanism works: It keeps a socket to an Apple server constantly connected over the mobile data interface, waiting for the server to say something.

So, in opposition to the (currently chosen) correct answer, I suggest that the answer to the asker's question: Does this routing means changing the WIFI to 3G or vice versa?? is actually, "Yes, sort of!" If the method returns true, the caller is assured that connections to that IP address will happen over the indicated interface.

And to Google: Boo on you for not documenting some of your APIs better!

Christo answered 27/7, 2012 at 16:12 Comment(1)
This is the correct answer, it should be the accepted one IMO. If you implement a method to use requestRouteToHost() and you turn off internet from your wireless network (like taking out the modem cable to your router) you will see that requestRouteToHost() will keep returning true when there is for sure no internet connection, but wireless is on (you only have connection within your wireless network and not to the outside)Kylakylah
M
0

Method requestRouteToHost() does not change wifi to 3G or vice versa!

Official Documentation :

public boolean requestRouteToHost (int networkType, int hostAddress) 

Ensure that a network route exists to deliver traffic to the specified host via the specified network interface. An attempt to add a route that already exists is ignored, but treated as successful.

  • Parameters

    networkType the type of the network over which traffic to the specified host is to be routed

    hostAddress the IP address of the host to which the route is desired

  • Returns

    true on success, false on failure

Marquettamarquette answered 3/8, 2011 at 7:59 Comment(4)
Thanks.. if i have given the IP address of particular website as the host address can i route this??Synchrotron
you can check if there is a route to the website or not (boolean)Marquettamarquette
@SherifelKhatib what do you mean when you say route to the website? What is route to website?Myelencephalon
@blackcrow route to the website is actually in this case just a boolean that explains whether the host is reachable. Consider it a ping.Marquettamarquette

© 2022 - 2024 — McMap. All rights reserved.