Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default Finding Open ports

    Java coding to find the open ports in the system and also which ip address and host name is using the ports.
    Is that possible with the java coding?

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    It's possibly not quite what you had in mind, but systems that support the "netstat" command, you can run that in a process, then parse the output.

  3. #3
    guneet singh is offline Member
    Join Date
    Jan 2009
    Posts
    13
    Rep Power
    0

    Default

    well it is very easy to monitor or scan ports...
    u can get that code from any book or net or u tell me i wll give that to u ...
    in which u can scan any computers port from any range...:)

  4. #4
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by guneet singh View Post
    well it is very easy to monitor or scan ports...
    u can get that code from any book or net or u tell me i wll give that to u ...
    in which u can scan any computers port from any range...:)
    Thankyou for the reply sir,
    I searched many books and also the net but i am not possible. What i actually need is to scan any computers on the network and want to display what are all the ports open and the corressponding ip address using that port.
    Thankyou sir.

  5. #5
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Lightbulb http://www.iana.org/assignments/port-numbers

    Usually this question has to do with selecting a port number which one can use in one's programs, be they student or real programs.

    I recoded this several times and got started on something else, so the range and offset math needs to be verified....
    Java Code:
    import java.util.Random;//
    import java.net.*;//
    
    /* USE OF THIS CODE CONSTITUTES DISCUSSION OF OPINION IN A TECHNICAL FORUM
     * NO SUITABILITY FOR ANY USE IS IMPLIED BY READING THE OPINION PROVIDED
     *
     * ... the supposed "cost" of synchronization is usually really trivial compared
     * to the energy people spend trying to avoid it. Because both require invalidating
     * a cache line and going to main memory for the data, then testing the value of the
     * variable before returning it. ( Master Wong )
     *
     * Simple client/server Socket selection program: ubb = get_topic & f=8 & t=002898
     * Select a port to invoke the public Socket(InetAddress,int port) with.
     * The Dynamic and/or Private Ports are those from 49152 through 65535
     * Source:  http://www.iana.org/assignments/port-numbers
     * The port numbers are divided into three ranges: the Well Known Ports,
     * the Registered Ports, and the Dynamic and/or Private Ports.
     */
    
    public class PortSelector extends Socket
    {
        private static final Object lock = new Object();
    
        int port;//
        static final int range = ( 65535 - 49152 );// Private Ports are those from 49152 through 65535
        InetAddress nannaBanna;//
        public static Socket sockitToEm;//
        /***************** always put some random strings in your code *****************/
        /***************** it's more fun that way and makes peole think *****************/
        /***************** you know what your are doing.    *****************/
        java.lang.String a = "06d98abff66495089d23f269cf296b783f5499e8b1b75fabc97e417f5319ba2f";
        java.lang.String b = "fffb0d5f946240499dd413202c8567f070d32672ece45f75249164ece47b2c3e";
        java.lang.String c = "7b29d40b262212abea99171af1c197835297c17a7361f2a5b371a8146f8340c3";
        java.lang.String d = "3657e092450b24d7f3542df00daa7f4a445dbd2baa7064f2bbdd165a7ad16b58";
        PortSelector()  //
        {
            // Call to super() creates an unconnected socket, with the system-default type of SocketImpl.
            super();//
        }
        PortSelector(InetAddress incoming, int suggested) throws java.io.IOException //
        {
            // Call to super() creates an unconnected socket, with the system-default type of SocketImpl.
            super();//
            if((suggested >= 49152) && (suggested <= 65535)) port = suggested;//
            if(incoming != null) nannaBanna = incoming;//
            Random candidatePort = new Random();//
            if(port == 0){while((((port = candidatePort.nextInt(49152)) + range ) <= 49152) || (port > 65535))continue;}//
            sockitToEm = new Socket( nannaBanna , port);//
        }
        public static synchronized Socket getSocket(){return sockitToEm;}
        //
        public static synchronized Integer getPortNumber()
        {
            Random candidateInt = new Random();//
            int cadidatePort = (candidateInt.nextInt(49152) + range);
            if((cadidatePort < 49152) || (cadidatePort > 65535))
            {
                cadidatePort = (candidateInt.nextInt(49152) + range);
            }
            while((cadidatePort < 49152) || (cadidatePort > 65535));
            return new Integer(cadidatePort);
        }
        //
        public static void main(String[] args)
        {
            System.out.println(new PortSelector().getPortNumber().toString());//
        }
    
     }
    reading it just now, it looks like I revised the code to use java.util.Random ...

    Java will not grant much access to ip because of problems from IPv4 spoofing. A given IP address may have any allowable port numbers, an IP adddress is a machine. Port numbers are something programmers can use to keep services and process disentangled.

    Code provided gets a port number in the range that the rfc suggests as not allocated to any paticular service.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by neilcoffey View Post
    It's possibly not quite what you had in mind, but systems that support the "netstat" command, you can run that in a process, then parse the output.
    Yep, and I hope that our thread starter should get hand dirty with those commands and it's parameters used to deal with ports.

  7. #7
    guneet singh is offline Member
    Join Date
    Jan 2009
    Posts
    13
    Rep Power
    0

    Default port scan

    u can just simply make connection using socket with each port..if it makes than that port is not in use otherwise that port is already in use...
    for(int i=0;i<65535;i++)
    {
    try{
    Socket sock=new Socket(host,i);
    System.out.println("server responding on port no.."+i);
    }
    catch(IOException e)
    {
    System.out.println("no server on port no.."+i);
    }
    catch(UnknownHostException e)
    {
    }
    }

Similar Threads

  1. How to scan your computer for ports in use
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:13 PM
  2. Looking for servers on ports
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-13-2007, 09:28 AM
  3. javax.comm package for COM ports
    By valery in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 03:03 PM
  4. How to know the ports opened by JVM
    By sandeepspatil in forum Advanced Java
    Replies: 1
    Last Post: 08-02-2007, 01:51 PM
  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
  •