0

I would want to use resolved IP address to connect to server instead of hostname. Here is my code snippets:

     // Get domain name from URL
    String domainName = url.substring("http://".length(),
            url.indexOf("/", 8));
    // Get IP address as string
    InetAddress inet = null;
    try {
        inet = InetAddress.getByName(domainName);
    } catch (UnknownHostException e) {
        Log.i(TAG, "The IP address cannot be resolved for " + domainName);
    }
        resolvedIP = inet.getHostAddress(); 

Here i'm able to successfully get IP ADDRESS. Now i try to replace my url as below:

url = url.replace(domainName, resolvedIP);

Now I connect to server:
URL download = new URL(url);


conn = (HttpURLConnection) url.openConnection();

conn.getInputStream(); //Throws IO Exception

I'm able to successfully connect if I use URL as it is (without replacing domain name with IP Address).

Please let me know if I'm doing things correctly.

Ramya K Sharma
  • 743
  • 8
  • 16
  • 1
    Sorry if it's a stupid question, but why do you need to do this? Using the URL with the hostname will do exactly the same as you're trying to do manually. – Mark Allison Jun 17 '11 at 07:42
  • The use case is: There are many servers configured behind a load balancer. And I try to connect multiple times. I want to do this to the same server I've first connected for some analysis purpose. I'm not sure if this is possible but logically it must be possible. Can I? – Ramya K Sharma Jun 17 '11 at 08:22
  • 2
    Well, that really depends on how the load balancer works. Unless you know for sure that it's doing DNS-based load balancing, this approach will not ensure that you get to the same server each time. – Mark Allison Jun 17 '11 at 08:26
  • Is there any other approach to ensure connecting to same server from client side? I just read about Sticky bit but, it is something the load-balancer handles. – Ramya K Sharma Jun 17 '11 at 10:15
  • You'll need to speak with whoever runs /owns the load balancer to find out how it works and if there's any way of ensuring that you get to that server repeatedly. – Mark Allison Jun 17 '11 at 10:50

2 Answers2

2

As a general rule, try to use the hostname-based URL. It is not uncommon to have multiple web sites on the same IP, distinguished only by the site name (so-called virtual hosting). What you are doing will only work for the case where there is a single site on a given IP.

It's quite common for load-balancers to have one IP for each "site" it balances, but there's still no guarantee that you will connect to the same backend for two consecutive requests. However, there are web load balancers that extend the use of virtual hosting all the way to the load-balancing layer.

I would simply try to stick downloading by a hostname-based URL.

Vatine
  • 20,782
  • 4
  • 54
  • 70
0

One solution to get around the VHost problem is to use your own DNS server and always return the same ip address for the host you're connecting to.

This requires no change to your application.

vipw
  • 7,593
  • 4
  • 25
  • 48