Guest User

Untitled

a guest
Apr 25th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. public static boolean isUp(String IP, int port) {
  2. Socket s = null;
  3. try {
  4. s = new Socket();
  5. s.setSoTimeout(10);
  6. s.connect(new InetSocketAddress(IP, port), 10);
  7. return true;
  8. } catch(IOException ex) {
  9. return false;
  10. } finally {
  11. if(s.isConnected()) {
  12. try {
  13. s.close();
  14. return true;
  15. } catch(IOException ex) {
  16. return false;
  17. }
  18. } else {
  19. return false;
  20. }
  21. }
  22. }
  23.  
  24. protected void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.activity_main);
  27.  
  28. TextView textView = findViewById(R.id.textView)
  29.  
  30. textView.setText("10.0.0.138:80 is " + (isUp("10.0.0.138", 80) ? "up" : "down") + "!")
  31. }
  32.  
  33. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  34. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  35. <uses-permission android:name="android.permission.INTERNET" />
Add Comment
Please, Sign In to add comment