Created
November 13, 2013 15:00
-
-
Save tonetheman/7450464 to your computer and use it in GitHub Desktop.
port checking in java nothing fancy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.net.Socket; | |
| public class PortCheck { | |
| int port = -1; | |
| public void info(String s) { | |
| System.out.println("INFO: " + s); | |
| } | |
| public void err(String s) { | |
| System.out.println("ERR: " + s); | |
| } | |
| public void checkPortAvailable(String server, int port) { | |
| info("testing port " + server + " " + port); | |
| Socket s = null; | |
| try { | |
| s = new Socket(server, port); | |
| } catch(Exception e) { | |
| err("1 " + e.getMessage()); | |
| } finally { | |
| if (s!=null) { | |
| try { | |
| s.close(); | |
| } catch(Exception e) { | |
| err("2 " + e.getMessage()); | |
| } | |
| } | |
| } | |
| } | |
| public void parseargs(String[] args) { | |
| for(int i=0;i<args.length;i++) { | |
| if ("-p".equals(args[i])) { | |
| port = Integer.parseInt(args[++i]); | |
| } | |
| } | |
| } | |
| public void announce() { } | |
| public void finish() { } | |
| public void run() { | |
| announce(); | |
| checkPortAvailable("localhost", port); | |
| checkPortAvailable("127.0.0.1", port); | |
| finish(); | |
| } | |
| public static void main(String[] args) { | |
| PortCheck pc = new PortCheck(); | |
| pc.parseargs(args); | |
| pc.run(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment