I'm trying to code a game server browser in java. I first retrieve the servers from an url (json) into an JsonArray
. Then I loop through and use HttpURLConnection
to retrieve the servers info (kills, map etc. that are also in JSON form) and finally I do this to calculate the ping:
request = (HttpURLConnection) new URL(serverAddress).openConnection();
request.setConnectTimeout(300);
request.setReadTimeout(300);
start = System.nanoTime();
request.connect();
ping = (System.nanoTime() - start) / 1000000;
Is this a correct way to calculate the ping? Is there a faster/efficient way to retrieve the json? It is really slow, is it possible to make more than 1 requests at a time? If yes, can you point me toward a way/link/tutorial that can help me.
Should I use a Socket instead? if yes this is what I have so far and is not working (trying to get the json into a string so I can parse it into an object)
InetAddress address = InetAddress.getByName(ip);
Socket socket = new Socket(address, port);
PrintWriter writer = new PrintWriter(socket.getOutputStream());
writer.print("GET / HTTP/1.1\r\n");
writer.print("Host: " + ip + ":" + port + "\r\n");
writer.print("\r\n");
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//DataInputStream dis = new DataInputStream(socket.getInputStream());
StringBuffer inputLine = new StringBuffer();
String line;
while ((line= br.readLine()) != null) {
inputLine.append(line);
System.out.println(line);
}
the servers have the following format : http://xx.xx.xx.xx:11800,
Thanks