2 Replies Latest reply: Mar 16, 2007 11:23 AM by 807597 RSS

    Port open or closed

    807597
      I need to write a program which tells weather a particular port , say 2800 is open or closed? how do i go about it?
        • 1. Re: Port open or closed
          807597
          hai i have the same problem , If u got the solution plz send me the code

          my rediff address is cuteassassin@rediffmail.com
          • 2. Re: Port open or closed
            807597
            The statement "port 2800 is open" simply means that there's an application or service running at the host machine listening on port 2800. What you want to do, is called "Port Scanning".

            Java does not support low level networking, so you have to try connecting to that port, whether it is open or closed. The following code will simply do this:
            Socket sock;
            try {
                    sock = new Socket("MyHostAddress",2800);
                    System.out.println("Ok, the port 2800 is open...");
            } catch(IOException err) {
                    System.out.println("Sorry, the port 2800 is closed...");
                    System.exit(0);
            }
            More advanced and faster port scanning methods require low level networking, and cannot be done with Java.