| | |
Help need with Socket, Thread, and BufferedReader
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2005
Posts: 3
Reputation:
Solved Threads: 0
I have until Monday at 3:00 pm to get this chat client working. I have been at this for 2 weeks, now. I have finally given in to asking for help. I'm trying to convert the following specs into code:
1. In the launchFrame method, you need to make the connection to the server(make a socket connection).
2. You need to get streams (input and output) from the socket connection so that you can send your messages to the server, and receive other messages from theserver.
3. You need a RemoteReader—a runnable object—which can be run by a thread and continually listen for input on the input stream you got from the socket.
4. You need to modify the actionPerformed method so that when the user clicks the “send� button you send the text of the message to the server (using the output stream) rather than putting it into the text area.
5. You need to modify the action listener on the “input� text field to do the same as above (send the message to the server).
6. You need to have the RemoteReader object put the incoming messages into the text area.
My problem is a couple of things. First, all thread examples I have looked at have loops in them. I understand the reason for the loop in the examples. But, I don't believe I need a loop in my thread. Because, I believe I have the thread to continue until all clients are closed. Second, I know I don't want to make my GUI the runnable object. But, I'm not sure if the run() should be the runnable object. Third, I think I have socket coded in the right place. But, not really sure on that. Fourth, the 4th and 5th step/point about the send button and enter from the text field. I'm not sure if that is suppose to be coming from the input/output stream or the socket itself or even the thread. Really having problems seeing how to code that. I have looked at the java docs. But, nothing seems to be what I need. Unless, I missed something. Which could be very possible at this point. Finally, the void main, I'm not sure I want it to still call the ChatClient class or the thread class. If it is the later then I need to know how to get the GUI created. I know this seems like I'm just rambling. But, I really could use the help. My current code is below. Please, help me.
1. In the launchFrame method, you need to make the connection to the server(make a socket connection).
2. You need to get streams (input and output) from the socket connection so that you can send your messages to the server, and receive other messages from theserver.
3. You need a RemoteReader—a runnable object—which can be run by a thread and continually listen for input on the input stream you got from the socket.
4. You need to modify the actionPerformed method so that when the user clicks the “send� button you send the text of the message to the server (using the output stream) rather than putting it into the text area.
5. You need to modify the action listener on the “input� text field to do the same as above (send the message to the server).
6. You need to have the RemoteReader object put the incoming messages into the text area.
My problem is a couple of things. First, all thread examples I have looked at have loops in them. I understand the reason for the loop in the examples. But, I don't believe I need a loop in my thread. Because, I believe I have the thread to continue until all clients are closed. Second, I know I don't want to make my GUI the runnable object. But, I'm not sure if the run() should be the runnable object. Third, I think I have socket coded in the right place. But, not really sure on that. Fourth, the 4th and 5th step/point about the send button and enter from the text field. I'm not sure if that is suppose to be coming from the input/output stream or the socket itself or even the thread. Really having problems seeing how to code that. I have looked at the java docs. But, nothing seems to be what I need. Unless, I missed something. Which could be very possible at this point. Finally, the void main, I'm not sure I want it to still call the ChatClient class or the thread class. If it is the later then I need to know how to get the GUI created. I know this seems like I'm just rambling. But, I really could use the help. My current code is below. Please, help me.
Java Syntax (Toggle Plain Text)
import java.io.*; import java.awt.*; import java.awt.event.*; import java.net.*; class ChatClient extends Frame { private TextField tf1 = new TextField("", 50); private TextArea ta; private Button sendButton; private Button quitButton; private Button okButton; private MenuItem exitMT, aboutMT; private Choice choice; private String user; private AboutDialog dialog; private Thread runner; private Socket s; private OutputStream os; private BufferedReader br; public ChatClient() { super("ChatClient"); runner = null; addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0); }}); addWindowListener(new WindowAdapter() {public void windowActivated(WindowEvent e) { tf1.requestFocusInWindow(); } }); setTitle("Chat Room"); Panel p = new Panel(new BorderLayout()); Panel p1 = new Panel(new BorderLayout()); Panel p2 = new Panel(new GridLayout(3,1)); MenuBar mb = new MenuBar (); setMenuBar (mb); Menu fileMenu = new Menu ("File"); mb.add(fileMenu); Menu helpMenu = new Menu ("Help"); mb.add(helpMenu); fileMenu.add (exitMT= new MenuItem ("Quit")); helpMenu.add (aboutMT = new MenuItem ("About")); exitMT.addActionListener(new ExitCommand()); aboutMT.addActionListener(new AboutCommand()); //creating main panel add(p, BorderLayout.NORTH); ta = new TextArea(10, 50); add(ta, BorderLayout.CENTER); ta.setSize(500,300); ta.setEditable(false); //adding buttons and choice box to the right side of the window sendButton = new Button("Send"); Panel p3 = new Panel(); // sendButton.setSize(15,15); p3.add(sendButton); p2.add(p3); sendButton.addActionListener(new CopyCommand()); quitButton = new Button("Quit"); Panel p4 = new Panel(); // quitButton.setSize(15,15); p4.add(quitButton); p2.add(p4); quitButton.addActionListener(new ExitCommand()); Panel p5 = new Panel(); choice = new Choice(); choice.addItem("John"); choice.addItem("Jena"); choice.addItem("Colleen"); choice.addItem("Steve"); user=choice.getItem(0); p5.add(choice); choice.addItemListener(new CopyCommand()); p2.add(p5); add(p2, BorderLayout.EAST); //adding the textfield to the south end of the window p1.add(tf1); add(p1, BorderLayout.SOUTH); tf1.addActionListener(new CopyCommand()); //connecting to the server try { s = new Socket("10.14.81.145", 7968); br = new BufferedReader( new InputStreamReader(s.getInputStream())); // os =s.getOutputStream(); } catch(Exception e) { System.out.println(e); System.out.println("exiting ..."); System.exit(0); } catch(IOException e) { e.printStackTrace(); } finally { s.close(); br.close(); os.close(); } } class ReadMessages implements Runnable { public void run() { //something having to do with bufferedreader and thread. I think! runner = new Thread(this); runner.start(); } } //handling ActionEvent from buttons and menu items class ExitCommand implements ActionListener { public void actionPerformed (ActionEvent e) { tf1.setText(""); ta.setText(""); choice.select(0); s.close(); br.close(); s.close(); System.exit(0); } } class AboutCommand implements ActionListener { public void actionPerformed(ActionEvent e) { String arg = e.getActionCommand(); if(arg.equals("About")) { if (dialog == null) // first time dialog = new AboutDialog(ChatClient.this); dialog.show(); } } } class AboutDialog extends Dialog { public AboutDialog(Frame parent) { super(parent, "About ChatClient", true); Panel p6 = new Panel(); p6.add(new Label("Chat Client ver 1.0")); p6.add(new Label("By Jennifer McAuliffe")); add(p6, "Center"); Panel p7 = new Panel(); Button ok = new Button("Ok"); p7.add(ok); add(p7, "South"); ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { setVisible(false); } } ); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { setVisible(false); } } ); setSize(220, 150); } } class CopyCommand implements ActionListener, ItemListener { public void itemStateChanged(ItemEvent e) { if (e.getSource() instanceof Choice) { user = (String)e.getItem(); } } public void actionPerformed (ActionEvent e) { //getting data from textfield to server and back //inserting server data to textarea ta.append(br.readLine()); //ta.append(user + ": " + tf1.getText() + "\n"); tf1.setText(""); tf1.requestFocus(); } public void keyPressed (KeyEvent ke) { //figure out if the enter key was pressed if (ke.getKeyCode() == KeyEvent.VK_ENTER) { //getting data from textfield to server and back //inserting server data to textarea ta.append(br.readLine()); //ta.append(user + ": " + tf1.getText() + "\n"); tf1.setText(""); tf1.requestFocus(); } } public void keyTyped (KeyEvent ke) { } public void keyReleased (KeyEvent ke) { } } public static void main(String[] args) { Runnable prog = new ReadMessages(); Frame f = new ChatClient(); f.setSize(600, 400); f.show(); } }
•
•
Join Date: Apr 2005
Posts: 3
Reputation:
Solved Threads: 0
Ok! I thought I had all my problems solved. The thing is that it didn't work. It actually made the program worse. So, I have gotten my program back to what it was last night. The thing is that I have another thought on the loop for the thread. I'm thinking it is needed to make sure the thread continuly reads the server when either the enter key is pressed or the send button is clicked. But, when you code for the loop there is a specified end to the loop. This is where I'm just a little confused or I'm just a really big ediot on the fact that I can't figure these 6 little steps. Thank you in advance for any help you can provide.
![]() |
Similar Threads
- creating an instant messenger program in java (Java)
- Moving beyond the Socket 370 Board (Motherboards, CPUs and RAM)
- Create Socket server and client? (C++)
- Making a Chatting Application (Java)
- Reading Mail (Java)
- Question about read method of InputStream (Java)
- java client server quiz. interesting!!! (Java)
Other Threads in the Java Forum
- Previous Thread: StreamTokenizer.TT_EOF???
- Next Thread: newbie... Java load large picture :) Lit'l help plez
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui homework html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql sqlite stop string swing testautomation threads time tree ui unicode validation windows





