Results 1 to 15 of 15
Thread: Program freezes please help
- 11-15-2012, 06:59 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
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 }
- 11-15-2012, 07:24 PM #2
Re: Program freezes please help
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.Java Code:s = ss.accept();
- 11-16-2012, 02:49 AM #3
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
Re: Program freezes please help
So how can I run that line and keep the program from stopping?
-
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.
- 11-16-2012, 04:38 AM #5
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
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:
Full code below.Java Code:try { ss.close(); l.stopThread(); messageDisplay.append("Server connection stopped.\n"); isConnected = false; portField.setEditable(true); }
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"); } }
-
Re: Program freezes please help
First get rid of this block:
You should never have something like this in your code. Ever. As all it does is prevent you from having to fix bad code.Java Code:catch(NullPointerException e) { }
- 11-16-2012, 04:44 AM #7
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
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?
- 11-16-2012, 04:46 AM #8
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
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.
- 11-16-2012, 04:47 AM #9
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.
-
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-16-2012, 04:48 AM #11
Re: Program freezes please help
Thats where the null pointer happens?It's pointing at line 244 s.close();
-
Re: Program freezes please help
Also, more organic is to use the form:
Java Code:if (foo != null) { foo.close(); }
- 11-16-2012, 04:49 AM #13
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?
- 11-16-2012, 04:50 AM #14
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
Re: Program freezes please help
Yes I see it. Thank you so much!!!
- 11-16-2012, 04:53 AM #15
Similar Threads
-
Jframe freezes while doing other task
By danoc93 in forum AWT / SwingReplies: 9Last Post: 05-14-2012, 03:14 AM -
java sql update freezes
By L19htn1n9 in forum Advanced JavaReplies: 5Last Post: 05-06-2012, 02:15 AM -
Eclipse freezes when opening SVN project
By kjkrum in forum EclipseReplies: 6Last Post: 11-23-2011, 01:21 AM -
Urgent Help Please. My program freezes... Was working fine...
By jMaster in forum New To JavaReplies: 4Last Post: 11-03-2011, 09:19 PM -
Server Socket GUI Freezes
By mrhid6 in forum Threads and SynchronizationReplies: 12Last Post: 09-30-2011, 04:39 PM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks