Results 1 to 8 of 8
  1. #1
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Ports being used by a service

    Hi,

    Im writing a port scanner program in java, and when run it determines any ports opened in the system. How can I find what service is using that port being opened?
    Thanks.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Ports being used by a service

    You could use JNI (or a library that use it) and use system services/files e.g. /etc/services
    Or if you have access to the internet you could ask IANA :D
    Java Code:
    		URL iana = new URL("http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml");
    		int port = 80;
    		String protocol = "tcp";
    		DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    		Document doc = db.parse(iana.openStream());
    		XPathExpression expr = XPathFactory.newInstance().newXPath().compile("//record[number="+port+" and protocol='"+protocol+"']/name"); //or/and description ...
    		Node name = (Node) expr.evaluate(doc, XPathConstants.NODE);
    		System.out.println(name.getTextContent()); //http

  3. #3
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Ports being used by a service

    Oh I see. Thanks. But what this is doing is, searching the web for the ports that can be used by services?
    What Im trying to do is to determine what opened ports are being used by what services on the local system. Because im only scanning my computer's ports, not remote computers.
    Thanks

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Ports being used by a service

    "what opened ports are being used by what services on the local system. Because im only scanning my computer's ports, not remote computers"
    its already clear to me, but about what services are you talking about? Well known ports? User ports? Dynamic ports? :D

  5. #5
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Ports being used by a service

    Hm I suppose all ports :D. Like all tcp and udp ports ranging from 1-65535. If you open up the command prompt and type in 'netstat -an', you will see all ports which are opened, and they are the ports that I'd like to find what services are using those ports?
    Thanks

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Ports being used by a service

    It is likely (unless there's a handy package somewhere in Java) that you'll have to do a Runtime.exec() using that command and then read the process stream to see what's returned. That might also be quicker than trying to find a proper Java package.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Ports being used by a service

    Oh yes, I actually have a method to take command String arguments which uses the command line and outputs the result in java. So I can use the command 'taskkill' to kill processes by their PID. But the porblem is, I dont know how to find out what ports they have opened? If I can find that command(s) to do so, that should be happy days :)
    Thanks

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Ports being used by a service

    That becomes an OS question, really, as that is entirely OS-dependent.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Ports
    By dougie1809 in forum Networking
    Replies: 2
    Last Post: 02-17-2012, 10:15 PM
  2. Replies: 3
    Last Post: 07-06-2010, 10:15 PM
  3. Socket's ports
    By Rose88 in forum Networking
    Replies: 5
    Last Post: 05-31-2010, 09:21 PM
  4. how to call service method inside another service
    By kirtichopra2003 in forum Web Frameworks
    Replies: 1
    Last Post: 10-24-2009, 02:19 AM
  5. Ports In Java
    By Peter in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 06:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •