Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, problems with SocketAddress and sockets. Reverse lookup?

i have a problem with Android. I am trying to connect to a server with a proxy with no luck.

I have this code that works fine on normal Java. It only defines a proxy server and creates a socket that would connect to google with that proxy. It sends a simple GET request and then shows the response.

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketAddress;

public class Main {

    public static void main(String[] args) {
        try{
            //create the proxy info
            SocketAddress socketAddress = new InetSocketAddress("78.137.18.67" , 8364);
            Proxy proxy = new Proxy(Proxy.Type.SOCKS, socketAddress);

            // create the socket with the proxy
            Socket socket = new Socket(proxy);

            // connect to some address and send/receive data
            socket.connect(new InetSocketAddress("www.google.com", 80));
            socket.getOutputStream().write("GET /index.html HTTP/1.1\r\nHost: www.google.com\r\n\r\n".getBytes("UTF-8"));
            byte result[] = new byte[1024];
            socket.getInputStream().read(result);
            socket.close();
            System.out.println(new String(result));
        }catch(Exception e){
            e.printStackTrace();
        }

    }
}

The problem with android, with a code similar like that, is that the InetSocketAddress is doing something strange. It seems that it does a reverse lookup of the given ip, and then the socket created with the proxy tries to connect with the resolved host name, in this case is 78-137-18-67.dynamic-pool.mclaut.net.

This would not be a problem (except on performance) if the socket could resolve the hostname back to the ip address. The fact is that this hostname cannot be resolved to ip address with my internet connection (i don't know if others can do). So the reverse lookup is working fine but the normal lookups fails, so when the socket tries to connect through the proxy it raises the following exception:

08-25 19:26:46.332: ERROR/Microlog(3526): 40274 SocketConnection

[ERROR] Error establishing connection java.net.SocketException: SOCKS connection failed: java.net.UnknownHostException: 78-137-18-67.dynamic-pool.mclaut.net

So the question is, why it is trying to connect with the hostname if i gave the ip address? Is there any way to avoid this lookup? I have tried with createUnresolved of InetSocketAddress but in this case the socket hangs on connection.

Is not a waste of time, internet connection, etc, to do a reverse DNS lookup to get the hostname (if any), and later when the socket needs to connect, resolve again the host to an ip address?

NOTE: this code is an example, the real app do not perform any http request in this way. It uses binary data packets.

like image 999
Alvaro Luis Bustamante Avatar asked Feb 02 '26 03:02

Alvaro Luis Bustamante


1 Answers

To prevent a reverse lookup, you can create the InetAddress with getByAddress(byte[]).

Then pass the InetAddress instance into the InetSocketAddress constructor.

Alternatively, use the factory method InetSocketAddress.createUnresolved(String,int)

like image 128
CrackerJack9 Avatar answered Feb 04 '26 21:02

CrackerJack9



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!