Help need with Socket, Thread, and BufferedReader

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2005
Posts: 3
Reputation: jmozzie is an unknown quantity at this point 
Solved Threads: 0
jmozzie jmozzie is offline Offline
Newbie Poster

Help need with Socket, Thread, and BufferedReader

 
0
  #1
Apr 23rd, 2005
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. import java.io.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.net.*;
  5.  
  6.  
  7. class ChatClient extends Frame
  8. {
  9. private TextField tf1 = new TextField("", 50);
  10. private TextArea ta;
  11. private Button sendButton;
  12. private Button quitButton;
  13. private Button okButton;
  14. private MenuItem exitMT, aboutMT;
  15. private Choice choice;
  16. private String user;
  17. private AboutDialog dialog;
  18.  
  19. private Thread runner;
  20. private Socket s;
  21. private OutputStream os;
  22. private BufferedReader br;
  23.  
  24.  
  25. public ChatClient()
  26. {
  27.  
  28. super("ChatClient");
  29.  
  30. runner = null;
  31.  
  32. addWindowListener(new WindowAdapter()
  33. {public void windowClosing(WindowEvent e) {System.exit(0); }});
  34. addWindowListener(new WindowAdapter()
  35. {public void windowActivated(WindowEvent e)
  36. {
  37. tf1.requestFocusInWindow();
  38. }
  39. });
  40.  
  41. setTitle("Chat Room");
  42. Panel p = new Panel(new BorderLayout());
  43. Panel p1 = new Panel(new BorderLayout());
  44. Panel p2 = new Panel(new GridLayout(3,1));
  45.  
  46.  
  47. MenuBar mb = new MenuBar ();
  48. setMenuBar (mb);
  49.  
  50. Menu fileMenu = new Menu ("File");
  51. mb.add(fileMenu);
  52.  
  53. Menu helpMenu = new Menu ("Help");
  54. mb.add(helpMenu);
  55.  
  56. fileMenu.add (exitMT= new MenuItem ("Quit"));
  57. helpMenu.add (aboutMT = new MenuItem ("About"));
  58.  
  59. exitMT.addActionListener(new ExitCommand());
  60. aboutMT.addActionListener(new AboutCommand());
  61.  
  62.  
  63. //creating main panel
  64. add(p, BorderLayout.NORTH);
  65. ta = new TextArea(10, 50);
  66. add(ta, BorderLayout.CENTER);
  67. ta.setSize(500,300);
  68. ta.setEditable(false);
  69.  
  70.  
  71. //adding buttons and choice box to the right side of the window
  72. sendButton = new Button("Send");
  73. Panel p3 = new Panel();
  74. // sendButton.setSize(15,15);
  75. p3.add(sendButton);
  76. p2.add(p3);
  77. sendButton.addActionListener(new CopyCommand());
  78. quitButton = new Button("Quit");
  79. Panel p4 = new Panel();
  80. // quitButton.setSize(15,15);
  81. p4.add(quitButton);
  82. p2.add(p4);
  83. quitButton.addActionListener(new ExitCommand());
  84. Panel p5 = new Panel();
  85. choice = new Choice();
  86. choice.addItem("John");
  87. choice.addItem("Jena");
  88. choice.addItem("Colleen");
  89. choice.addItem("Steve");
  90. user=choice.getItem(0);
  91. p5.add(choice);
  92. choice.addItemListener(new CopyCommand());
  93. p2.add(p5);
  94.  
  95. add(p2, BorderLayout.EAST);
  96.  
  97. //adding the textfield to the south end of the window
  98.  
  99. p1.add(tf1);
  100. add(p1, BorderLayout.SOUTH);
  101.  
  102. tf1.addActionListener(new CopyCommand());
  103.  
  104.  
  105. //connecting to the server
  106. try
  107. {
  108. s = new Socket("10.14.81.145", 7968);
  109. br = new BufferedReader(
  110. new InputStreamReader(s.getInputStream()));
  111.  
  112. // os =s.getOutputStream();
  113. }
  114. catch(Exception e)
  115. {
  116. System.out.println(e);
  117. System.out.println("exiting ...");
  118. System.exit(0);
  119. }
  120. catch(IOException e)
  121. {
  122. e.printStackTrace();
  123. }
  124. finally
  125. {
  126. s.close();
  127. br.close();
  128. os.close();
  129. }
  130.  
  131.  
  132. }
  133.  
  134. class ReadMessages implements Runnable
  135. {
  136. public void run()
  137. {
  138. //something having to do with bufferedreader and thread. I think!
  139. runner = new Thread(this);
  140. runner.start();
  141.  
  142. }
  143. }
  144.  
  145. //handling ActionEvent from buttons and menu items
  146. class ExitCommand implements ActionListener
  147. {
  148. public void actionPerformed (ActionEvent e)
  149. {
  150. tf1.setText("");
  151. ta.setText("");
  152. choice.select(0);
  153. s.close();
  154. br.close();
  155. s.close();
  156. System.exit(0);
  157.  
  158. }
  159. }
  160.  
  161. class AboutCommand implements ActionListener
  162. {
  163. public void actionPerformed(ActionEvent e)
  164. { String arg = e.getActionCommand();
  165. if(arg.equals("About"))
  166. { if (dialog == null) // first time
  167. dialog = new AboutDialog(ChatClient.this);
  168. dialog.show();
  169. }
  170. }
  171. }
  172.  
  173. class AboutDialog extends Dialog
  174. {
  175. public AboutDialog(Frame parent)
  176. {
  177. super(parent, "About ChatClient", true);
  178. Panel p6 = new Panel();
  179. p6.add(new Label("Chat Client ver 1.0"));
  180. p6.add(new Label("By Jennifer McAuliffe"));
  181. add(p6, "Center");
  182.  
  183. Panel p7 = new Panel();
  184. Button ok = new Button("Ok");
  185. p7.add(ok);
  186. add(p7, "South");
  187.  
  188. ok.addActionListener(new ActionListener() { public void
  189. actionPerformed(ActionEvent evt) { setVisible(false); } } );
  190.  
  191. addWindowListener(new WindowAdapter() { public void
  192. windowClosing(WindowEvent e) { setVisible(false); } } );
  193.  
  194. setSize(220, 150);
  195.  
  196. }
  197. }
  198.  
  199. class CopyCommand implements ActionListener, ItemListener
  200. {
  201. public void itemStateChanged(ItemEvent e)
  202. { if (e.getSource() instanceof Choice)
  203. {
  204. user = (String)e.getItem();
  205. }
  206. }
  207.  
  208. public void actionPerformed (ActionEvent e)
  209. {
  210. //getting data from textfield to server and back
  211. //inserting server data to textarea
  212. ta.append(br.readLine());
  213. //ta.append(user + ": " + tf1.getText() + "\n");
  214. tf1.setText("");
  215. tf1.requestFocus();
  216. }
  217.  
  218. public void keyPressed (KeyEvent ke)
  219. {
  220. //figure out if the enter key was pressed
  221. if (ke.getKeyCode() == KeyEvent.VK_ENTER)
  222. {
  223.  
  224. //getting data from textfield to server and back
  225. //inserting server data to textarea
  226. ta.append(br.readLine());
  227. //ta.append(user + ": " + tf1.getText() + "\n");
  228. tf1.setText("");
  229. tf1.requestFocus();
  230. }
  231. }
  232.  
  233. public void keyTyped (KeyEvent ke) { }
  234.  
  235. public void keyReleased (KeyEvent ke) { }
  236.  
  237. }
  238.  
  239. public static void main(String[] args)
  240. {
  241. Runnable prog = new ReadMessages();
  242. Frame f = new ChatClient();
  243. f.setSize(600, 400);
  244. f.show();
  245. }
  246.  
  247. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 3
Reputation: jmozzie is an unknown quantity at this point 
Solved Threads: 0
jmozzie jmozzie is offline Offline
Newbie Poster

Re: Help need with Socket, Thread, and BufferedReader

 
0
  #2
Apr 23rd, 2005
Never mind on the help! I think I got all my problems figured out. Thanks any way.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 3
Reputation: jmozzie is an unknown quantity at this point 
Solved Threads: 0
jmozzie jmozzie is offline Offline
Newbie Poster

Re: Help need with Socket, Thread, and BufferedReader

 
0
  #3
Apr 24th, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC