Results 1 to 15 of 15
Like Tree3Likes
  • 2 Post By Fubarable
  • 1 Post By quad64bit

Thread: Program freezes please help

  1. #1
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    0

    Default Program freezes please help

    I am writing a server program and whenever it gets to line 35 it freezes without displaying the previous line:
    while(true)
    {
    s = ss.accept();
    messageDisplay.append("Connection to "+s+" established.\n");

    }

    Code is below. Any help would be appreciated. Thanks.

    Java Code:
    import java.io.*;
    import java.net.*;
    
    public class Server extends javax.swing.JFrame
    {
        private int port;
        private boolean isConnected = false;
        private ServerSocket ss;
        private Socket s;
        
        public static void main(String args[])
        {        
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                   new Server().setVisible(true);
                }
            });
        }
        
         public Server()
        {
            initComponents();
            
        }
         
         private void listen(int port)
         {
             try
             {
                ss = new ServerSocket(port);
                messageDisplay.append("Connected to port: "+port+"\n");
                while(true)
                {
                    s = ss.accept();
                    messageDisplay.append("Connection to "+s+" established.\n");
                    
                }
                
             }
             catch(Exception e)
             {
                 messageDisplay.append("Failed to connect. Invalid Port number.\n");
             }
         }
         
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents()
        {
    
            jScrollPane1 = new javax.swing.JScrollPane();
            messageDisplay = new javax.swing.JTextArea();
            portLabel = new javax.swing.JLabel();
            portField = new javax.swing.JTextField();
            startButton = new javax.swing.JButton();
            stopButton = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setBackground(new java.awt.Color(204, 204, 204));
    
            messageDisplay.setEditable(false);
            messageDisplay.setColumns(20);
            messageDisplay.setLineWrap(true);
            messageDisplay.setRows(5);
            messageDisplay.setWrapStyleWord(true);
            jScrollPane1.setViewportView(messageDisplay);
    
            portLabel.setText("Port:");
    
            portField.setText("50000");
            portField.addActionListener(new java.awt.event.ActionListener()
            {
                public void actionPerformed(java.awt.event.ActionEvent evt)
                {
                    portFieldActionPerformed(evt);
                }
            });
    
            startButton.setText("Start");
            startButton.addActionListener(new java.awt.event.ActionListener()
            {
                public void actionPerformed(java.awt.event.ActionEvent evt)
                {
                    startButtonActionPerformed(evt);
                }
            });
    
            stopButton.setText("Stop");
            stopButton.addActionListener(new java.awt.event.ActionListener()
            {
                public void actionPerformed(java.awt.event.ActionEvent evt)
                {
                    stopButtonActionPerformed(evt);
                }
            });
    
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(29, 29, 29)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 529, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(layout.createSequentialGroup()
                            .add(portLabel)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(portField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 118, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(18, 18, 18)
                            .add(startButton)
                            .add(18, 18, 18)
                            .add(stopButton)))
                    .addContainerGap(25, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(28, 28, 28)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 363, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(18, 18, 18)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(portLabel)
                        .add(portField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(startButton)
                        .add(stopButton))
                    .addContainerGap(19, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void startButtonActionPerformed(java.awt.event.ActionEvent evt)                                            
        {                                                
            // TODO add your handling code here:
            if(isConnected==false)
            {
                try
                {
                    port = Integer.parseInt(portField.getText());
                    portField.setEditable(false);
                    messageDisplay.append("Connecting...\n");
                    isConnected = true;
                    listen(port);
                    
                }
                catch(Exception e)
                {
                    messageDisplay.append("Failed to connect. Invalid Port number\n");
                    
                }
            }
        }                                           
    
        private void stopButtonActionPerformed(java.awt.event.ActionEvent evt)                                           
        {                                               
            // TODO add your handling code here:
            if(isConnected==true)
            {
                try
                {
                     s.close();
                     ss.close();
               
                }
                catch(IOException e)
                {
                     messageDisplay.append("Failed to stop server connection.\n");
                     e.printStackTrace();
                }
                catch(NullPointerException e)
                {
                
                }
    
                messageDisplay.append("Server connection stopped.\n");
                isConnected = false;
                portField.setEditable(true);
            }
        }                                          
    
        private void portFieldActionPerformed(java.awt.event.ActionEvent evt)                                          
        {                                              
            // TODO add your handling code here:
        }                                         
    
        
        // Variables declaration - do not modify                     
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextArea messageDisplay;
        private javax.swing.JTextField portField;
        private javax.swing.JLabel portLabel;
        private javax.swing.JButton startButton;
        private javax.swing.JButton stopButton;
        // End of variables declaration                   
    }

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Program freezes please help

    Java Code:
     s = ss.accept();
    That is a blocking operation. Code execution will stop there until the server socket receives a connection attempt. Once a client connects to this server, that method will return the socket for the open connection.

  3. #3
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    0

    Default Re: Program freezes please help

    So how can I run that line and keep the program from stopping?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Program freezes please help

    As with any program, if you want to run things concurrently, you need to use threading. Google the tutorial titled, "Concurrency in Swing" to find out more on this.

  5. #5
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    0

    Default Re: Code not executing.

    Thank you for your help guys. I got that part working thanks to your comments. I am having a new problem now. There is a part of the code that is not executing when the stop button is pressed. Starting on line 161 just after the ss.close(). The rest of that code doesn't execute:

    snippet:

    Java Code:
    try
                {
                     ss.close();
                     l.stopThread();
                     messageDisplay.append("Server connection stopped.\n");
                     isConnected = false;
                     portField.setEditable(true);
                      
                
                }
    Full code below.

    Java Code:
    import java.io.*;
    import java.net.*;
    
    public class Server extends javax.swing.JFrame
    {
        private static int port;
        private static boolean isConnected = false;
        public static ServerSocket ss;
        public static Socket s;
        public Listener l;
        public boolean closeListener = false;
        private Thread lThread;
        
        
        public static void main(String args[])
        {        
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                   new Server().setVisible(true);
                   
                }
            });
            
                    
        }
        
         public Server()
        {
            initComponents();
            
        }
         
         
         
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents()
        {
    
            jScrollPane1 = new javax.swing.JScrollPane();
            messageDisplay = new javax.swing.JTextArea();
            portLabel = new javax.swing.JLabel();
            portField = new javax.swing.JTextField();
            startButton = new javax.swing.JButton();
            stopButton = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setBackground(new java.awt.Color(204, 204, 204));
    
            messageDisplay.setEditable(false);
            messageDisplay.setColumns(20);
            messageDisplay.setLineWrap(true);
            messageDisplay.setRows(5);
            messageDisplay.setWrapStyleWord(true);
            jScrollPane1.setViewportView(messageDisplay);
    
            portLabel.setText("Port:");
    
            portField.setText("50000");
            portField.addActionListener(new java.awt.event.ActionListener()
            {
                public void actionPerformed(java.awt.event.ActionEvent evt)
                {
                    portFieldActionPerformed(evt);
                }
            });
    
            startButton.setText("Start");
            startButton.addActionListener(new java.awt.event.ActionListener()
            {
                public void actionPerformed(java.awt.event.ActionEvent evt)
                {
                    startButtonActionPerformed(evt);
                }
            });
    
            stopButton.setText("Stop");
            stopButton.addActionListener(new java.awt.event.ActionListener()
            {
                public void actionPerformed(java.awt.event.ActionEvent evt)
                {
                    stopButtonActionPerformed(evt);
                }
            });
    
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(29, 29, 29)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 529, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(layout.createSequentialGroup()
                            .add(portLabel)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(portField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 118, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(18, 18, 18)
                            .add(startButton)
                            .add(18, 18, 18)
                            .add(stopButton)))
                    .addContainerGap(25, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(28, 28, 28)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 363, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(18, 18, 18)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(portLabel)
                        .add(portField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(startButton)
                        .add(stopButton))
                    .addContainerGap(19, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>
    
        private void startButtonActionPerformed(java.awt.event.ActionEvent evt)                                            
        {                                                
            // TODO add your handling code here:
            if(isConnected==false)
            {
                try
                {
                    messageDisplay.append("Connecting...\n");
                    port = Integer.parseInt(portField.getText());
                    portField.setEditable(false);
                    ss = new ServerSocket(port);
                    messageDisplay.append("Connected to Port: "+port+".\n");
                    
                    isConnected = true;
                    l = new Listener(ss, this);
                    lThread = new Thread(l);
                    lThread.start();
                    
                    
                    
                }
                catch(Exception e)
                {
                    messageDisplay.append("Invalid Port number.\n");
                    isConnected = false;
                    portField.setEditable(true);
                    
                }
            }
        }                                           
    
        private void stopButtonActionPerformed(java.awt.event.ActionEvent evt)                                           
        {                                               
            // TODO add your handling code here:
            if(isConnected==true)
            {
                
                try
                {
                     ss.close();
                     l.stopThread();
                     messageDisplay.append("Server connection stopped.\n");
                     isConnected = false;
                     portField.setEditable(true);
                     
               
                }
                catch(IOException e)
                {
                     messageDisplay.append("Failed to stop server connection.\n");
                     e.printStackTrace();
                }
                catch(NullPointerException e)
                {
                
                }
    
                
            }
        }                                          
    
        private void portFieldActionPerformed(java.awt.event.ActionEvent evt)                                          
        {                                              
            // TODO add your handling code here:
        }                                         
    
        
        // Variables declaration - do not modify
        private javax.swing.JScrollPane jScrollPane1;
        public static javax.swing.JTextArea messageDisplay;
        private javax.swing.JTextField portField;
        private javax.swing.JLabel portLabel;
        private javax.swing.JButton startButton;
        private javax.swing.JButton stopButton;
        // End of variables declaration
    }
    
    class Listener implements Runnable
    {
        private volatile Thread blinker;
        public Socket s;
        private ServerSocket ss;
        private Server server;
        
        public Listener(ServerSocket ss, Server server)
        {
            this.ss = ss;
            this.server = server;
            
        }
        
        public void run()
        {
            Thread thisThread = Thread.currentThread();
           
           while(blinker==thisThread)
           {
            while(true)
                {
                   server.messageDisplay.append("Waiting for Client connection.\n");
                   try
                    {
                        
                        s = ss.accept();
                        server.messageDisplay.append("Connected to "+s+".\n");
                
                    }
                    catch(IOException e)
                    {
                      server.messageDisplay.append("Client connection "+s+" failed to connect.\n");
                    }
                    
                }
           }
        }
        
        public void stopThread()
        {
            try
            {
                s.close();
            }
            catch(IOException e)
            {
                server.messageDisplay.append("Failed to close client connection.\n");
            }
            blinker = null;
            server.messageDisplay.append("Client connection closed.\n");
        }
        
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Program freezes please help

    First get rid of this block:
    Java Code:
                catch(NullPointerException e)
                {
                 
                }
    You should never have something like this in your code. Ever. As all it does is prevent you from having to fix bad code.

  7. #7
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    0

    Default Re: Program freezes please help

    Thank you for pointing that out but it seems the Null Pointer Exception may be the problem because as soon as I press the stop button, thats the error I get. What can I do there?

  8. #8
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    0

    Default Re: Program freezes please help

    It's pointing at line 244 s.close();

    Edit: I added an if(s!=null) above that statement and it seems to work now. Thank you. You guys are my heroes.

  9. #9
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Program freezes please help

    Give us your complete stack trace. It simply means the program is trying to read something from, or use a method of a variable which is currently null, for whatever reason. the stack trace will show us where, and from that we can deduce why.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Program freezes please help

    That's why you should never have that block of code that I told you to remove. Now look back in your code to see why s might be null.

  11. #11
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Program freezes please help

    It's pointing at line 244 s.close();
    Thats where the null pointer happens?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Program freezes please help

    Also, more organic is to use the form:

    Java Code:
    if (foo != null) {
       foo.close();
    }
    quad64bit and Jingoism like this.

  13. #13
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Program freezes please help

    Oh yeah, so, if ss is still in it's blocking state waiting for a connection, then until that happens, s will be null because it was never initialized by ss on line 37. Can you see why?
    Fubarable likes this.

  14. #14
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    0

    Default Re: Program freezes please help

    Yes I see it. Thank you so much!!!

  15. #15
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Program freezes please help

    Hang in there, threading and socket programming is fun stuff when you finally see how it all works!

Similar Threads

  1. Jframe freezes while doing other task
    By danoc93 in forum AWT / Swing
    Replies: 9
    Last Post: 05-14-2012, 03:14 AM
  2. java sql update freezes
    By L19htn1n9 in forum Advanced Java
    Replies: 5
    Last Post: 05-06-2012, 02:15 AM
  3. Eclipse freezes when opening SVN project
    By kjkrum in forum Eclipse
    Replies: 6
    Last Post: 11-23-2011, 01:21 AM
  4. Replies: 4
    Last Post: 11-03-2011, 09:19 PM
  5. Server Socket GUI Freezes
    By mrhid6 in forum Threads and Synchronization
    Replies: 12
    Last Post: 09-30-2011, 04:39 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
  •