Skip to content

Instantly share code, notes, and snippets.

@Double0negative
Last active December 12, 2015 08:08
Show Gist options
  • Save Double0negative/4741381 to your computer and use it in GitHub Desktop.
Save Double0negative/4741381 to your computer and use it in GitHub Desktop.
import java.io.DataInputStream;
import java.io.OutputStream;
import java.net.Socket;
public class Pinger {
@SuppressWarnings("deprecation")
public static int [] ping(String ip, int port){
try{
Socket sk = new Socket(ip,port);
OutputStream out = sk.getOutputStream();
out.write(0xFE);
out.flush();
DataInputStream in = new DataInputStream(sk.getInputStream());
String s = in.readLine();
String s1 = "";
for(int a = 0; a<s.length();a+=2){
s1 = s1 + s.charAt(a);
}
String[] s2 = s1.split("§");
sk.close();
//return [online],[max]
return new int [] {Integer.parseInt(s2[s2.length-2]), Integer.parseInt(s2[s2.length-1])};
}
catch(Exception e){
//server is offline/unreachable
return new int []{-1,-1};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment