Results 1 to 3 of 3
Thread: Chat GUI timing problem
- 09-08-2010, 11:38 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 3
- Rep Power
- 0
Chat GUI timing problem
Hello all,
This is my first post, so please go easy on a newb :)
I am studying Java, and I have an assignment to write a chat program, using 2 programs, with sockets, etc. (I should also say that the program does not use threads, that is my next topic)
I have written a program that runs from the command line, with no problems whatsoever. However my next task is to adapt the client program, so that it uses a GUI. I am stuck on one thing, and it's got me tearing my hair out:confused:
When I run both programs my GUI appears, and I enter my message. But the text does not appear in the JTextArea, until the message from the server comes back. I want the first message to be displayed in the JtextArea when it is first typed in by the user.
I figure it has something to do with the fact that the Server socket is waiting to receive a message, but I've tried changing things around a bit to no avail.
here is the code-
server -
the client GUI-Java Code:import java.io.*; import java.net.*; public class HSChat1 { private static final int intPortNumber = 4321; public static void main(String args[]) { PrintWriter pWriter= null; BufferedReader bReader = null; BufferedReader bReader2 = null; ServerSocket serverSocket = null; Socket clientSocket = null; String inputLine; String message = " "; String compString = "quit"; try { serverSocket = new ServerSocket(intPortNumber); } catch (IOException e) { System.err.println("Could not listen on port: 4321."); System.err.println(e.getMessage()); System.exit(1); } // close Catch(IOException) block try { System.out.println("The Chat Server is running"); clientSocket = serverSocket.accept(); pWriter = new PrintWriter( clientSocket.getOutputStream(), true); bReader = new BufferedReader( new InputStreamReader(clientSocket.getInputStream())); bReader2 = new BufferedReader(new InputStreamReader(System.in)); inputLine = bReader.readLine(); while (!(inputLine.equals(compString))) { System.out.print("\nClient: "); System.out.println(inputLine); System.out.print("\nYou: "); message = bReader2.readLine(); pWriter.println(message); pWriter.flush(); if(!message.equals(compString)) { inputLine = bReader.readLine(); } else { inputLine = "quit"; pWriter.println(inputLine); pWriter.flush(); } } bReader.close(); bReader2.close(); pWriter.close(); clientSocket.close(); serverSocket.close(); } catch (IOException e) { System.err.println("Accept failed."); System.exit(1); } // close catch(IOException) block } //close main method } //close public class
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; public class GUITester { //create JPanel primary; JTextField jtfInput; JTextArea jtAreaOutput; String text = " "; String newline = " "; String serverMessage = " "; private static final int intPortNumber = 4321; private static final String address = "127.0.0.1"; public static PrintWriter pWriter = null; public static BufferedReader bReader2 = null; public static Socket clientSocket = null; public String compString = "quit"; public GUITester() // Set up the GUI { jtfInput = new JTextField(20); jtfInput.addActionListener(new TextListener()); jtAreaOutput = new JTextArea(5, 20); JButton quit = new JButton ("Quit"); quit.addActionListener(new ButtonListener()); jtAreaOutput.setCaretPosition(jtAreaOutput.getDocument().getLength()); jtAreaOutput.setEditable(false); JScrollPane scrollPane = new JScrollPane(jtAreaOutput, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); GridLayout gridLO = new GridLayout(2,3); primary = new JPanel(); primary.setLayout (gridLO); primary.add(jtfInput); primary.add(scrollPane); primary.add(quit); } public static void main(String args[]) { try // create socket, reader & writer { clientSocket = new Socket(address, intPortNumber); pWriter = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream())); bReader2 = new BufferedReader( new InputStreamReader(clientSocket.getInputStream())); } catch(IOException ioe) { System.out.println("There was an Input/output problem"); System.out.println(ioe.getMessage()); } // close catch(IOException) block JFrame chatFrame = new JFrame("Chat Client"); chatFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GUITester myChatGUI = new GUITester(); chatFrame.getContentPane().add (myChatGUI.getPanel()); chatFrame.pack(); chatFrame.setVisible(true); } //Close main() method public JPanel getPanel() { return primary; } private class TextListener implements ActionListener { public void actionPerformed(ActionEvent evt) { text = jtfInput.getText(); jtAreaOutput.append("\nYou: "); jtAreaOutput.append(text + newline); jtfInput.selectAll(); try { pWriter.println(text); pWriter.flush(); serverMessage = bReader2.readLine(); if(!serverMessage.equals(compString)) { jtAreaOutput.append("\nServer: "); jtAreaOutput.append(serverMessage); } else { System.exit(1); } } catch(IOException ioe) { System.out.println("There was an Input/output problem"); System.out.println(ioe.getMessage()); } // close catch(IOException) block } } private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent evt) { text = "quit"; jtAreaOutput.append(text); System.out.println("Closing"); System.exit(1); } } } //close class
Please excuse my lack of commenting. I hope the code is reasonably clear. I still have work to do on other stuff, like the quit button, but I don't think that will be a problem.
Any advice, clues etc will be gratefully accepted. Thanks in anticipation,
Justin
-
I don't know how you can do this program successfully without using threading as your current problem is due to just this-- no background thread to prevent the buffered reader's readline from blocking the GUI's thread.
Also, your code is not well indented making it too difficult to read. You may want to fix this by using consistent indenting and by changing tabs to spaces (the forum handles this better).
Best of luck.
- 09-08-2010, 11:28 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
URLConnection not timing out as expected
By StephenS in forum NetworkingReplies: 2Last Post: 04-02-2010, 03:10 AM -
Problem With Chat
By Noelf21 in forum New To JavaReplies: 1Last Post: 12-15-2009, 02:51 PM -
custom midlet-servlet chat app problem
By vukdavid in forum CLDC and MIDPReplies: 0Last Post: 09-22-2009, 12:30 PM -
Chat application with RMI
By dhaval.rajput.p in forum Advanced JavaReplies: 0Last Post: 07-11-2009, 10:12 AM -
Timing array loop performance
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks