Making a Chatting Application

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

Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Making a Chatting Application

 
0
  #1
Aug 5th, 2005
Hi Everyone,

I'm making a Java chat application, but it freezes whenever I click "Connect." My code is attached below. Thanks.

  1. import javax.swing.*;
  2. import javax.swing.event.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import java.net.*;
  7.  
  8. public class CinnaChat extends JFrame
  9. {
  10.  
  11. JLabel lblServerIP = new JLabel("Server IP:");
  12. JTextField serverIP = new JTextField(15);
  13.  
  14. JLabel lblPort = new JLabel("Port Num:");
  15. JTextField port = new JTextField(15);
  16.  
  17. JLabel lblName = new JLabel("NickName:");
  18. JTextField name = new JTextField(15);
  19.  
  20. JRadioButton serverBTN = new JRadioButton("Server (Host)");
  21. JRadioButton clientBTN = new JRadioButton("Client (Guest)");
  22.  
  23. JButton connect = new JButton("Connect");
  24. JButton disconnect = new JButton("Disconnect");
  25.  
  26. JTextArea messages = new JTextArea("",10,21);
  27. JTextField mssg = new JTextField(15);
  28. JButton send = new JButton("Send");
  29.  
  30. Font btn = new Font("times new roman",Font.BOLD,20);
  31.  
  32. Socket skt = null;
  33. Socket skt2 = null;
  34. ServerSocket srvr = null;
  35.  
  36. boolean server = true;
  37.  
  38. public CinnaChat()
  39. {
  40. super("CinnaChat");
  41. setSize(500,250);
  42. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  43. setVisible(true);
  44.  
  45. Container contentArea = getContentPane();
  46. contentArea.setBackground(Color.lightGray);
  47.  
  48. GridLayout flowManager = new GridLayout(1,2);
  49. contentArea.setLayout(flowManager);
  50.  
  51. JPanel pnl = new JPanel();
  52. pnl.add(lblServerIP);
  53. pnl.add(serverIP);
  54. pnl.add(lblPort);
  55. pnl.add(port);
  56. pnl.add(lblName);
  57. pnl.add(name);
  58. ButtonGroup type = new ButtonGroup();
  59. type.add(serverBTN);
  60. serverBTN.addActionListener(
  61. new ActionListener()
  62. {
  63. public void actionPerformed(ActionEvent event)
  64. {
  65. server = true;
  66. }
  67. }
  68. );
  69. type.add(clientBTN);
  70. clientBTN.addActionListener(
  71. new ActionListener() {
  72. public void actionPerformed(ActionEvent event) {
  73. server = false;
  74. }
  75. }
  76. );
  77. pnl.add(serverBTN);
  78. pnl.add(clientBTN);
  79. pnl.add(connect);
  80. connect.addActionListener(
  81. new ActionListener()
  82. {
  83. public void actionPerformed(ActionEvent event)
  84. {
  85. connect();
  86. }
  87. }
  88. );
  89. connect.setFont(btn);
  90. pnl.add(disconnect);
  91. disconnect.addActionListener(
  92. new ActionListener()
  93. {
  94. public void actionPerformed(ActionEvent event)
  95. {
  96. disconnect();
  97. }
  98. }
  99. );
  100.  
  101. disconnect.setFont(btn);
  102. contentArea.add(pnl);
  103.  
  104. JPanel pnl2 = new JPanel();
  105. pnl2.add(new JScrollPane(messages,
  106. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  107. JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS));
  108. pnl2.add(mssg);
  109. pnl2.add(send);
  110. send.addActionListener(
  111. new ActionListener()
  112. {
  113. public void actionPerformed(ActionEvent event)
  114. {
  115. send();
  116. }
  117. }
  118. );
  119.  
  120. contentArea.add(pnl2);
  121.  
  122. setContentPane(contentArea);
  123. }
  124.  
  125. public void connect()
  126. {
  127. try
  128. {
  129. if(server)
  130. {
  131. int portNum = Integer.parseInt(port.getText());
  132. srvr = new ServerSocket(portNum);
  133. skt = srvr.accept();
  134. messages.append("\nServer, "+name.getText()+", has connected.\n\n");
  135. BufferedReader in = new BufferedReader(new
  136. InputStreamReader(skt.getInputStream()));
  137. while (!in.ready()) {}
  138. messages.append(in.readLine()+"\n");
  139. in.close();
  140.  
  141. skt.close();
  142. srvr.close();
  143. }
  144. else
  145. {
  146. int portNum = Integer.parseInt(port.getText());
  147. skt2 = new Socket(serverIP.getText(), portNum);
  148. BufferedReader in = new BufferedReader(new
  149. InputStreamReader(skt2.getInputStream()));
  150. while (!in.ready()) {}
  151. messages.append(in.readLine()+"\n");
  152. in.close();
  153.  
  154. }
  155. }
  156. catch(Exception e1)
  157. {
  158. JOptionPane.showMessageDialog(null, "Error: Connection failed.\n"+
  159. "Please retry. Check IP Adress\n"+
  160. "and the port number.", "Error",
  161. JOptionPane.ERROR_MESSAGE);
  162. }
  163. }
  164.  
  165. public void disconnect()
  166. {
  167. try{
  168. skt.close();
  169. skt2.close();
  170. srvr.close();
  171. }
  172. catch(Exception e2)
  173. {
  174. }
  175. }
  176.  
  177. public void send()
  178. {
  179. try
  180. {
  181. if(server)
  182. {
  183. PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
  184. messages.append(name.getText() + ": " + mssg.getText() + "\n");
  185. out.print(name.getText() + ": " + mssg.getText() + "\n");
  186. out.close();
  187. }
  188. else
  189. {
  190. PrintWriter out = new PrintWriter(skt2.getOutputStream(), true);
  191. messages.append(name.getText() + ": " + mssg.getText() + "\n");
  192. out.print(name.getText() + ": " + mssg.getText() + "\n");
  193. out.close();
  194. }
  195. }
  196. catch(Exception e3)
  197. {
  198. }
  199. }
  200.  
  201. public static void main (String [] args)
  202. {
  203. new CinnaChat();
  204. }
  205. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: Making a Chatting Application

 
0
  #2
Aug 7th, 2005
if anybody has any advice, I would appreciate it.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Making a Chatting Application

 
0
  #3
Aug 7th, 2005
You got a lot to learn about multithreading and network programming I see.
Your server can accept only a single connection, ever.
Your client waits forever for input and as soon as it gets anything closes the stream.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: Making a Chatting Application

 
0
  #4
Aug 7th, 2005
thanks, jwenting! so basically, i have to open and close the socket for every message? Thanks. I also updated my code. The server side works, but when I try to get the client working, it freezes up. Thanks.

  1. import javax.swing.*;
  2. import javax.swing.event.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import java.net.*;
  7.  
  8. public class CinnaChat extends JFrame
  9. {
  10.  
  11. JLabel lblServerIP = new JLabel("Server IP:");
  12. JTextField serverIP = new JTextField(15);
  13.  
  14. JLabel lblPort = new JLabel("Port Num:");
  15. JTextField port = new JTextField(15);
  16.  
  17. JLabel lblName = new JLabel("NickName:");
  18. JTextField name = new JTextField(15);
  19.  
  20. JRadioButton serverBTN = new JRadioButton("Server (Host)");
  21. JRadioButton clientBTN = new JRadioButton("Client (Guest)");
  22.  
  23. JButton connect = new JButton("Connect");
  24. JButton disconnect = new JButton("Disconnect");
  25.  
  26. JTextArea messages = new JTextArea("",10,21);
  27. JTextField mssg = new JTextField(15);
  28. JButton send = new JButton("Send");
  29.  
  30. JLabel lbl = new JLabel("CinnaChat");
  31.  
  32. Font btn = new Font("times new roman",Font.BOLD,20);
  33. Font lblFont = new Font("times new roman",Font.BOLD+Font.HANGING_BASELINE,45);
  34.  
  35. Socket socket = null;
  36. Socket socket2 = null;
  37. ServerSocket serverSocket = null;
  38. PrintWriter out = null;
  39. BufferedReader in = null;
  40. PrintWriter out2 = null;
  41. BufferedReader in2 = null;
  42.  
  43. boolean server = true;
  44.  
  45. Color purple = new Color(108,111,206);
  46. Color yellow = new Color(173,115,5);
  47. Color blue = new Color(15,11,102);
  48.  
  49. public CinnaChat()
  50. {
  51. super("CinnaChat");
  52. setSize(505,250);
  53. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54. setVisible(true);
  55.  
  56. Container contentArea = getContentPane();
  57.  
  58. GridLayout flowManager = new GridLayout(1,2);
  59. contentArea.setLayout(flowManager);
  60.  
  61. Cursor cursor = new Cursor(Cursor.HAND_CURSOR);
  62. setCursor(cursor);
  63.  
  64. JPanel pnl = new JPanel();
  65. pnl.add(lbl);
  66. lbl.setFont(lblFont);
  67. lbl.setForeground(Color.darkGray);
  68. pnl.add(lblServerIP);
  69. lblServerIP.setForeground(purple);
  70. pnl.add(serverIP);
  71. pnl.add(lblPort);
  72. lblPort.setForeground(purple);
  73. pnl.add(port);
  74. pnl.add(lblName);
  75. lblName.setForeground(purple);
  76. pnl.add(name);
  77. ButtonGroup type = new ButtonGroup();
  78. type.add(serverBTN);
  79. serverBTN.setForeground(yellow);
  80. serverBTN.addActionListener(
  81. new ActionListener()
  82. {
  83. public void actionPerformed(ActionEvent event)
  84. {
  85. server = true;
  86. }
  87. }
  88. );
  89. type.add(clientBTN);
  90. clientBTN.setForeground(yellow);
  91. clientBTN.addActionListener(
  92. new ActionListener() {
  93. public void actionPerformed(ActionEvent event) {
  94. server = false;
  95. }
  96. }
  97. );
  98. pnl.add(serverBTN);
  99. pnl.add(clientBTN);
  100. pnl.add(connect);
  101. connect.addActionListener(
  102. new ActionListener()
  103. {
  104. public void actionPerformed(ActionEvent event)
  105. {
  106. connect();
  107. }
  108. }
  109. );
  110. connect.setFont(btn);
  111. connect.setForeground(blue);
  112. pnl.add(disconnect);
  113. disconnect.setEnabled(false);
  114. disconnect.addActionListener(
  115. new ActionListener()
  116. {
  117. public void actionPerformed(ActionEvent event)
  118. {
  119. disconnect();
  120. }
  121. }
  122. );
  123. disconnect.setFont(btn);
  124. disconnect.setForeground(blue);
  125. contentArea.add(pnl);
  126.  
  127. JPanel pnl2 = new JPanel();
  128. pnl2.add(new JScrollPane(messages,
  129. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  130. JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS));
  131. messages.setEditable(false);
  132. messages.setCursor(new Cursor(Cursor.TEXT_CURSOR));
  133. pnl2.add(mssg);
  134. pnl2.add(send);
  135. send.setEnabled(false);
  136. send.addActionListener(
  137. new ActionListener()
  138. {
  139. public void actionPerformed(ActionEvent event)
  140. {
  141. send();
  142. }
  143. }
  144. );
  145.  
  146. contentArea.add(pnl2);
  147.  
  148. contentArea.setBackground(Color.white);
  149.  
  150. setContentPane(contentArea);
  151. }
  152.  
  153. public void connect()
  154. {
  155. serverBTN.setEnabled(false);
  156. clientBTN.setEnabled(false);
  157. disconnect.setEnabled(true);
  158. connect.setEnabled(false);
  159. send.setEnabled(true);
  160. try
  161. {
  162.  
  163. if(!server)
  164. {
  165. try
  166. {
  167. socket = new Socket(serverIP.getText(),
  168. Integer.parseInt(port.getText()));
  169. out = new PrintWriter(socket.getOutputStream(), true);
  170. in = new BufferedReader(new InputStreamReader(
  171. socket.getInputStream()));
  172. messages.append(in.readLine());
  173. }
  174. catch (Exception hostEx)
  175. {
  176. JOptionPane.showMessageDialog(null,"Error with port number or IP "+
  177. "Address.","Error",
  178. JOptionPane.ERROR_MESSAGE);
  179. }
  180.  
  181. }
  182. else
  183. {
  184. try
  185. {
  186. serverSocket = new ServerSocket(Integer.parseInt(port.getText()));
  187. }
  188. catch (Exception portInUseEx)
  189. {
  190. JOptionPane.showMessageDialog(null,"Port in use. Select a different "+
  191. "port.","Port in Use",
  192. JOptionPane.ERROR_MESSAGE);
  193. }
  194. PrintWriter out2 = new PrintWriter(
  195. socket2.getOutputStream(), true);
  196. BufferedReader in2 = new BufferedReader(
  197. new InputStreamReader(
  198. socket2.getInputStream()));
  199. messages.append(in2.readLine());
  200. }
  201.  
  202. }
  203. catch (Exception exception)
  204. {
  205.  
  206. }
  207. }
  208.  
  209. public void disconnect()
  210. {
  211. serverBTN.setEnabled(true);
  212. clientBTN.setEnabled(true);
  213. disconnect.setEnabled(false);
  214. connect.setEnabled(true);
  215. send.setEnabled(false);
  216. try
  217. {
  218. if(!server)
  219. {
  220. out.close();
  221. in.close();
  222. socket.close();
  223. }
  224. else
  225. {
  226. out2.close();
  227. in2.close();
  228. socket2.close();
  229. serverSocket.close();
  230. }
  231. }
  232. catch(Exception closeEx)
  233. {
  234. }
  235. }
  236.  
  237. public void send()
  238. {
  239. try
  240. {
  241. if(!server)
  242. {
  243. messages.append(name.getText() + ": " + mssg.getText() + "\n");
  244. out.println(name.getText() + ": " + mssg.getText());
  245. }
  246. else
  247. {
  248. messages.append(name.getText() + ": " + mssg.getText() + "\n");
  249. out2.println(name.getText() + ": " + mssg.getText());
  250. }
  251. }
  252. catch(Exception e3)
  253. {
  254. }
  255. }
  256.  
  257. public static void main (String [] args)
  258. {
  259. new CinnaChat();
  260. }
  261. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 8
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: Making a Chatting Application

 
0
  #5
Aug 7th, 2005
Hi everyone,

Originally Posted by Ghost
basically, i have to open and close the socket for every message
Ghost, you really have to read the API docs. Once you diconnect a socket you cannot reconnect it and in your case your socket is a global one thus the socket does not work properly.

For the rest of my answer i have already answered it at this thread

http://wizardsolutionsusa.com/progra...topic.php?t=10

Richard West
Microsoft uses "One World, One Web, One Program" as a slogan.
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond

Tell me what type of software do you like and what would you pay for it

http://www.daniweb.com/techtalkforums/thread19660.html
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: Making a Chatting Application

 
0
  #6
Aug 7th, 2005
Would not making it global and passing it through functions help?

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Making a Chatting Application

 
0
  #7
Aug 7th, 2005
Take Sun course SL-275. It builds something like what you're trying to do as its final project.
Course takes a week, that phase takes about half a day.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 8
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: Making a Chatting Application

 
0
  #8
Aug 10th, 2005
Hi everyone,

Ghost if you really want to build your own chat application using sockets then check the below thread it comes with full source code and explanations

http://wizardsolutionsusa.com/progra...topic.php?t=55

If you have any comments post at the above link. You don't have to be a member to post at the above link. Youy can post as a guest

Thank You

Yours Sincerely

Richard West
Microsoft uses "One World, One Web, One Program" as a slogan.
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond

Tell me what type of software do you like and what would you pay for it

http://www.daniweb.com/techtalkforums/thread19660.html
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC