0

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

M. Joe
  • 13
  • 3

2 Answers2

0

I would suggest the second ( TCPSocket based )approach since you will get away with the overhead of creating a HTTP wrapper over the network object.

Regarding creating JSON from the read input string you can use any JSON Serializer and pass your read string to it so as to create JSONObject. Use gson ( a light weight JSON Serializer)

Gson gson = new Gson();
JsonElement element = gson.fromJson (tmp, JsonElement.class);
JsonObject object = element.getAsJsonObject();

In your code please try:

InetAddress address = InetAddress.getByName(ip);
Socket socket = new Socket(address, port);
BufferedReader br1 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//DataInputStream dis = new DataInputStream(socket.getInputStream());

StringBuffer inputLine = new StringBuffer();
String tmp;
while ((tmp = br1.readLine()) != null) {
    inputLine.append(tmp);
    System.out.println(tmp);
}
piy26
  • 1,574
  • 11
  • 21
  • Do you see wht's wrong with the Socket code I posted? I get a loop and nothing printed. – M. Joe Apr 24 '18 at 09:01
  • Please see the corrected code in the answer. DataInputStream dis is trying to read from the stream that's associated with br1. You need to use either one of them. – piy26 Apr 24 '18 at 09:06
  • It's just stuck in the while not even looping – M. Joe Apr 24 '18 at 09:16
  • If that's the case then you need to create a well formed request body i.e. HTTP Method and Header. No doubt this can be done in using PrintWriter and then calling write method but will be complex and prone to errors. If that's the case then you should approach via HTTPConnection. Please clarify your objective since there are 2 different problems with each having a different solution to solve optimally "Is this a correct way to calculate the ping? Is there a faster/efficient way to retrieve the json?" – piy26 Apr 24 '18 at 09:36
0

Speaking about the ping measurement I assume you want to know the numbers dynamically, not only on the connect moment.

InetAddress.getByName(host).isReachable(timeOut) will produce ICMP request.

You can check Brian Agnew response here How to measure response-time of network using Java?.

Anton Maximov
  • 251
  • 2
  • 6