Guest User

Untitled

a guest
Nov 8th, 2021
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1.     private static boolean testConn(String host, int port)
  2.  
  3.     {
  4.  
  5.         // Java Socket default timeout value: 60000 milliseconds
  6.  
  7.         Socket  socket = null;
  8.  
  9.        
  10.  
  11.         try {
  12.  
  13.             socket  = new Socket(host, port) ;
  14.  
  15.              return true;
  16.  
  17.  
  18.  
  19.         } catch(ConnectException e){
  20.  
  21.             System.out.println("testConn ConnectException, host:"+host+", port:"+port);
  22.  
  23.             return false;
  24.  
  25.         } catch (IOException e) {
  26.  
  27.             System.out.println("testConn IOException, host:"+host+", port:"+port);
  28.  
  29.             return false;
  30.  
  31.         }catch (Exception e) {
  32.  
  33.             System.out.println("testConn Exception, host:"+host+", port:"+port);
  34.  
  35.             return false;
  36.  
  37.         }finally{
  38.  
  39.             try {
  40.  
  41.                 socket.close();
  42.  
  43.             } catch (IOException e) {
  44.  
  45.                 System.out.println("testConn socket close fail");
  46.  
  47.             }
  48.  
  49.         }      
  50.  
  51.     }
Advertisement
Add Comment
Please, Sign In to add comment