SHARE
TWEET

Port Scanner

zDarkB0y_ Sep 21st, 2019 131 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package me.darkboy.portscanner;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.DataOutputStream;
  5. import java.io.InputStreamReader;
  6. import java.net.InetSocketAddress;
  7. import java.net.Socket;
  8.  
  9. public class Main {
  10.  
  11.     public static void main(String[] args) {
  12.         for (int i = 10000; i <= 65535; i++) {
  13.             scan("localhost", i);
  14.         }
  15.     }
  16.  
  17.     private static void scan(String ip, int port) {
  18.         boolean serverUp;
  19.         String[] serverData;
  20.         String rawServerData;
  21.         try {
  22.             Socket clientSocket = new Socket();
  23.             clientSocket.connect(new InetSocketAddress(ip, port), 5);
  24.             DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream());
  25.             BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  26.             byte[] payload = {(byte) 0xFE, (byte) 0x01};
  27.             dos.write(payload, 0, payload.length);
  28.             rawServerData = br.readLine();
  29.             clientSocket.close();
  30.         } catch (Exception e) {
  31.             System.out.println("Server Is Offline!");
  32.             return;
  33.         }
  34.  
  35.         if (rawServerData == null) {
  36.             serverUp = false;
  37.         } else {
  38.             serverData = rawServerData.split("\u0000\u0000\u0000");
  39.             serverUp = serverData.length >= 6;
  40.         }
  41.  
  42.         if (serverUp) {
  43.             System.out.println("Server Is Online!");
  44.         } else {
  45.             System.out.println("Server Is Offline!");
  46.         }
  47.     }
  48. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top