CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2011
    Posts
    3

    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
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Port Scanning ERROR

    Post the relevant bits of code in code tags and someone will look at it. Many people here won't look at linked files.

    It would also be helpful if the code is formatted, conforms to Java naming standards and contains comments describing what it is supposed to be doing
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Apr 2011
    Posts
    3

    Re: Port Scanning ERROR

    Ok here's the program code. The problem i'm having is that the program doesn't even seem to scan for the ports being open or not. It just shows all of them open if it can resolve the address given.
    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
     */
    
    package javaapplication9;
    
    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">                          
        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>                        
    
        private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
           jRadioButton2.setEnabled(false); // TODO add your handling code here:
        }                                             
    
        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:
        }                                       
    
        private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                              
    jRadioButton1.setEnabled(false);        // TODO add your handling code here:
        }                                             
    
        /**
        * @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                     
        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                   
    
    
    
    }

  4. #4
    Join Date
    Apr 2011
    Posts
    3

    Re: Port Scanning ERROR

    Here's the code. The problem i'm facing is that upon clicking the SCAN button, the program just doesnt seem to scan for the ports. It just displays ALL the ports in the range as open, if it can connect to the given address. Can anyone help me know where i'm going wrong?

    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
     */
    
    package javaapplication9;
    
    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">                          
        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>                        
    
        private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
           jRadioButton2.setEnabled(false); // TODO add your handling code here:
        }                                             
    
        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:
        }                                       
    
        private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                              
    jRadioButton1.setEnabled(false);        // TODO add your handling code here:
        }                                             
    
        /**
        * @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                     
        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                   
    
    
    
    }

  5. #5
    Join Date
    Mar 2011
    Location
    Dunwoody GA
    Posts
    8

    Re: Port Scanning ERROR

    If you're using NetBeans you should be able to set a break point on
    Code:
    s = new Socket(addr,i)
    run debug and then inspect your addr and i variables when the debugger breaks. They're probably not what you are expecting...
    Ray

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Port Scanning ERROR

    You shouldn't be doing the port scan on the event dispatch thread as it makes the GUI freeze and prevents you from stopping the process. Look at using a SwingWorker.

    The if statement in the following code is superfluous. s will always be non null as you have just created an instance of a Socket and assigned it to s.

    Code:
                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 ");
    And what happened to the properly formatted code I requested you post? The code in the button1ActionPerformed(..) method is a formatting mess which makes it incredibly difficult to read.

    You said you had a command line version that worked, can you post that code as well.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured