0

I am trying to get a data response using a socket. I need any kind of response from the DNS. So I try requesting the IP of "www.youtube.com" from google DNS. I am not getting any response.

Network network = ...
String DNS = "8.8.8.8";
try (Socket socket = network.getSocketFactory().createSocket(DNS, 53)) {
    socket.connect(new InetSocketAddress("www.youtube.com", 80));
    InputStreamReader input = new InputStreamReader(socket.getInputStream());
    BufferedReader reader = new BufferedReader(input);
    socket.getOutputStream().write("www.youtube.com".getBytes());
    String skip = reader.readLine(), string = reader.readLine();
    Log.e("READ", skip + " " + string);
} catch (IOException exception) {
    String message = context.getText(R.string.nointernet) + ": " +
    exception.getLocalizedMessage();
    Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    context.getVariables().setNetwork(null);
}

In this case, the log is always printing null which means I haven't communicated with the DNS server. How can a make the socket request?

Bret Joseph
  • 401
  • 3
  • 13
  • 1
    I'm not sure what the idea behind your code is but it has nothing to do with DNS. DNS lookup's requires to contact a DNS server, construct DNS requests according to the DNS standard, getting server response and parsing it according to the DNS standard. – Steffen Ullrich Jan 01 '23 at 06:22
  • I was trying not to use InetAdresss since it can work offline. I am just trying to check if a socket can give a response – Bret Joseph Jan 01 '23 at 06:31
  • you can just do `InetAddress.getByName("www.youtube.com").getAddress()` - but that will obviously not work if offline (unless eventually the DNS is running on the local machine) – user16320675 Jan 01 '23 at 09:46
  • @user16320675 i think getAddress is the right call here. thanks – Bret Joseph Jan 01 '23 at 10:28

0 Answers0