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?