Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Port Scanning ERROR

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Port Scanning ERROR

    Hey guys I'm making a port scanner for a project of mine. Made the required form nd stuff. But while doing the "meat" of the coding, the output isnt certainly what I wanted.

    While scanning for ports, my program shows the result as "Port x is open". It does so for all ports. Its like its not even scanning for open/closed ports. The code which I'm using WORKS when I compile it as a command-line program, but not so when running it as a form through Netbeans. Can anyone help me with this?
    Attached Files Attached Files


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Port Scanning ERROR

    Hello dbz_sid, welcome to the Java Programming Forums.

    Please don't post duplicate threads. I have deleted your other thread.

    Please also post your code within the highlight tags (see my signature)
    It makes things easier and people are more likely to reply.

    For the benefit of anyone reading this thread, here is the attached code:

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /*
     * PortScan.java
     *
     * Created on Apr 22, 2011, 5:03:32 PM
     */
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.SpinnerNumberModel;
    import javax.swing.*;
    import javax.swing.border.Border;
    import java.lang.Object;
    import java.net.*;
    import java.io.*;
    /**
     *
     * @author Siddharth
     */
    public class PortScan extends javax.swing.JFrame {
        int portStart;
            int portStop;
            int i;
                    String host;
            static boolean close=false;
                       /** Creates new form PortScan */
        public PortScan() {
            initComponents();
     
        }
     
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
        private void initComponents() {
     
            buttonGroup1 = new javax.swing.ButtonGroup();
            buttonGroup2 = new javax.swing.ButtonGroup();
            jSpinner2 = new javax.swing.JSpinner();
            jRadioButton1 = new javax.swing.JRadioButton();
            jRadioButton2 = new javax.swing.JRadioButton();
            label2 = new java.awt.Label();
            jLabel5 = new javax.swing.JLabel();
            label1 = new java.awt.Label();
            textField1 = new java.awt.TextField();
            textArea1 = new java.awt.TextArea();
            jLabel2 = new javax.swing.JLabel();
            jLabel6 = new javax.swing.JLabel();
            button2 = new java.awt.Button();
            jSpinner1 = new javax.swing.JSpinner();
            jProgressBar1 = new javax.swing.JProgressBar();
            jLabel1 = new javax.swing.JLabel();
            button1 = new java.awt.Button();
            jLabel4 = new javax.swing.JLabel();
            label3 = new java.awt.Label();
            button3 = new java.awt.Button();
            jLabel3 = new javax.swing.JLabel();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            jSpinner2.setFont(new java.awt.Font("Times New Roman 14", 1, 12));
            jSpinner2.setModel(new SpinnerNumberModel(65536, 1, 65536, 1));
     
            jRadioButton1.setFont(new java.awt.Font("Times New Roman 14", 1, 14)); // NOI18N
            jRadioButton1.setText("IP ADDRESS");
            jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton1ActionPerformed(evt);
                }
            });
     
            jRadioButton2.setFont(new java.awt.Font("Times New Roman 14", 1, 14));
            jRadioButton2.setText("HOST ADDRESS");
            jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton2ActionPerformed(evt);
                }
            });
     
            label2.setFont(new java.awt.Font("Times New Roman 14", 1, 12));
     
            jLabel5.setFont(new java.awt.Font("Times New Roman 14", 1, 14));
            jLabel5.setText("PROGRESS");
     
            label1.setFont(new java.awt.Font("Times New Roman 14", 1, 12));
     
            jLabel2.setFont(new java.awt.Font("Times New Roman 14", 1, 12));
            jLabel2.setText("TO");
     
            jLabel6.setFont(new java.awt.Font("Times New Roman", 1, 14));
            jLabel6.setText("OPEN PORTS");
     
            button2.setFont(new java.awt.Font("Times New Roman", 1, 14));
            button2.setLabel("STOP");
     
            jSpinner1.setFont(new java.awt.Font("Times New Roman 14", 1, 12));
            jSpinner1.setModel(new SpinnerNumberModel(1, 1, 65536, 1));
     
            jLabel1.setFont(new java.awt.Font("Times New Roman 14", 1, 14));
            jLabel1.setText("PORT SCANNER");
     
            button1.setFont(new java.awt.Font("Times New Roman", 1, 14));
            button1.setLabel("SCAN");
            button1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    button1ActionPerformed(evt);
                }
            });
     
            jLabel4.setFont(new java.awt.Font("Times New Roman 14", 1, 14));
            jLabel4.setText("ELAPSED TIME:");
     
            label3.setFont(new java.awt.Font("Times New Roman", 1, 14));
     
            button3.setFont(new java.awt.Font("Times New Roman", 1, 14));
            button3.setLabel("DNS ADDRESS RETRIEVAL");
     
            jLabel3.setFont(new java.awt.Font("Times New Roman 14", 1, 14));
            jLabel3.setText("PORT PROGRESS:");
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(113, 113, 113)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(textArea1, javax.swing.GroupLayout.PREFERRED_SIZE, 513, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(69, 69, 69)
                            .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(33, 33, 33)
                            .addComponent(jLabel2)
                            .addGap(34, 34, 34)
                            .addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, 485, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel5)
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(46, 46, 46)
                                    .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(24, 24, 24)
                                    .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 467, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addGap(67, 67, 67)
                            .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jRadioButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(169, 169, 169)
                            .addComponent(jRadioButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel6)
                            .addGap(95, 95, 95)
                            .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(34, 34, 34)
                            .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(86, 86, 86)
                            .addComponent(button2, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(98, 98, 98)
                            .addComponent(button3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGap(442, 442, 442))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(51, 51, 51)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jRadioButton1)
                        .addComponent(jRadioButton2))
                    .addGap(55, 55, 55)
                    .addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(66, 66, 66)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(55, 55, 55)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(label1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(34, 34, 34)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(35, 35, 35)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(42, 42, 42)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel6)
                        .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(47, 47, 47)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(button2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(button3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(38, 38, 38)
                    .addComponent(textArea1, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(821, 821, 821))
            );
     
            pack();
        }// </editor-fold>//GEN-END:initComponents
     
        private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
           jRadioButton2.setEnabled(false); // TODO add your handling code here:
        }//GEN-LAST:event_jRadioButton1ActionPerformed
     
        private void button1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button1ActionPerformed
     	host = textField1.getText();
     
           portStart = ((Integer)jSpinner1.getValue());
           portStop = ((Integer)jSpinner2.getValue());
           try{
               InetAddress addr = InetAddress.getByName(textField1.getText());
    textArea1.setText("The IP Address of the node is " +addr.getHostAddress() );
           for(i=portStart;i<=portStop;i++)
           {
     
               Socket s = null;
            try{
                s = new Socket(addr,i);
     
                 	if(s== null)textArea1.append("\n"+ "Port "+i+" is open."+"\n" );
                    else
                   textArea1.append("Port " + i + " is not in use ");
                            } catch (IOException ex) {
                   textArea1.append("Port " + i + " is not in use " );
                }
               /*finally{
                    try {
                            if (s != null) s.close();
                        }
                    catch (IOException ex) {
                   textArea1.append("Port " + i + " is not in use " );
                }*/
     
     
                //catch (UnknownHostException ex) {
                //textArea1.append("Host not reachable");
            }}catch (IOException ex) {
                   textArea1.append("Port " + i + " is not in use " );}
     
     
     
     
     
     
     
     
     
     
     
            // TODO add your handling code here:
        }//GEN-LAST:event_button1ActionPerformed
     
        private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
    jRadioButton1.setEnabled(false);        // TODO add your handling code here:
        }//GEN-LAST:event_jRadioButton2ActionPerformed
     
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new PortScan().setVisible(true);
                }
            });
        }
     
        // Variables declaration - do not modify//GEN-BEGIN:variables
        private java.awt.Button button1;
        private java.awt.Button button2;
        private java.awt.Button button3;
        private javax.swing.ButtonGroup buttonGroup1;
        private javax.swing.ButtonGroup buttonGroup2;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JLabel jLabel6;
        private javax.swing.JProgressBar jProgressBar1;
        private javax.swing.JRadioButton jRadioButton1;
        private javax.swing.JRadioButton jRadioButton2;
        private javax.swing.JSpinner jSpinner1;
        private javax.swing.JSpinner jSpinner2;
        private java.awt.Label label1;
        private java.awt.Label label2;
        private java.awt.Label label3;
        private java.awt.TextArea textArea1;
        private java.awt.TextField textField1;
        // End of variables declaration//GEN-END:variables
     
    }

    I have compiled this myself and scanned an IP address. It tells me that none of the ports are open. So I get a different result to what you are experiencing..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Port Scanning ERROR

    Oops. Sorry I made a mistake while sending you the attachments, I didnt save the file to its most recent version. Anyways, the changes I made to the code were in the ActionPerformed code of SCAN button
    Here's the corresponding code...

    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
     	host = textField1.getText();
     
           portStart = ((Integer)jSpinner1.getValue());
           portStop = ((Integer)jSpinner2.getValue());
           try{
               InetAddress addr = InetAddress.getByName(textField1.getText());
    textArea1.setText("The IP Address of the node is " +addr.getHostAddress() );
           for(i=portStart;i<=portStop;i++)
           {
     
               Socket s = null;
            try{
                s = new Socket(addr,i);
     
                 	if(s!=null)textArea1.append("\n"+ "Port "+i+" is open."+"\n" );
                    else
                   textArea1.append("Port " + i + " is not in use ");
                            } catch (IOException ex) {
                   textArea1.append("Port " + i + " is not in use " );
                }
               /*finally{
                    try {
                            if (s != null) s.close();
                        }
                    catch (IOException ex) {
                   textArea1.append("Port " + i + " is not in use " );
                }*/
     
     
                //catch (UnknownHostException ex) {
                //textArea1.append("Host not reachable");
            }}catch (IOException ex) {
                   textArea1.append("Port " + i + " is not in use " );}
     
     
     
     
     
     
     
     
     
     
     
            // TODO add your handling code here:
        }

    Now can anyone help me identify where I'm going wrong???

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Port Scanning ERROR

    Haha I'm dealing with the same problem! I didnt see your topic before so I created a topic basically with the same trouble but my code is different...
    I hope that somebody can help us! : )

    But anyway I'm still looking for a solution if you make this work please let me know!
    Last edited by tunai; September 26th, 2011 at 10:27 PM.

  5. #5
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Port Scanning ERROR

    s = new Socket(addr,i);

    if(s!=null)
    How often - to the nearest integer per year - do Java constructors return null?

    Why are you assuming that if the Socket(InetAddress, int) constructor returns an object it means the port is open? Does the API documentation say that? What other methods in the Socket class might help you test to see whether the port is open?

Similar Threads

  1. help w/ storing/scanning numbers in two dimensional arrays
    By robertsbd in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 1st, 2010, 11:56 PM
  2. help w/ storing/scanning numbers in arrays
    By robertsbd in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 17th, 2010, 10:55 PM
  3. Relay data from remote port to local port
    By chegers in forum Java Networking
    Replies: 0
    Last Post: November 7th, 2010, 12:46 PM
  4. Port issue?
    By Brt93yoda in forum Java Theory & Questions
    Replies: 3
    Last Post: October 13th, 2010, 05:28 PM
  5. Scanning Document Issue
    By redvenice in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 20th, 2010, 09:18 AM