Networked chat application between two clients

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

Join Date: Feb 2008
Posts: 88
Reputation: knight fyre is an unknown quantity at this point 
Solved Threads: 1
knight fyre's Avatar
knight fyre knight fyre is offline Offline
Junior Poster in Training

Networked chat application between two clients

 
0
  #1
Jun 11th, 2008
I'm trying to create a client/server setup that allows two clients to communicate with each other. The problem is that when I run the application the the message does not go to the send client, only to the server. Here is the source code.

Class MorseServer
  1. import javax.swing.JTextArea;
  2. import javax.swing.JFrame;
  3. import javax.swing.JScrollPane;
  4.  
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7.  
  8. import java.util.concurrent.ExecutorService;
  9. import java.util.concurrent.Executors;
  10. import java.util.concurrent.locks.Lock;
  11. import java.util.concurrent.locks.ReentrantLock;
  12. import java.util.concurrent.locks.Condition;
  13.  
  14. import java.io.IOException;
  15.  
  16. import java.io.ObjectInputStream;
  17. import java.io.ObjectOutputStream;
  18.  
  19. import javax.swing.SwingUtilities;
  20.  
  21.  
  22. public class MorseServer extends JFrame
  23. {
  24. private JTextArea serverDisplay;
  25. private ServerSocket server; // server socket to connect with users
  26. private ExecutorService messageExchange;
  27. private Lock messageLock;
  28. private Condition userTwoConnect;
  29. private Client[] client;
  30. private boolean twoUserConnected = false;
  31. private static String messageBuffer;
  32.  
  33. public MorseServer()
  34. {
  35. super("Morse Code Server");
  36.  
  37. serverDisplay = new JTextArea( "Waiting for connection\n" );
  38. serverDisplay.setEditable( false );
  39. add( new JScrollPane( serverDisplay ) );
  40.  
  41. messageExchange = Executors.newFixedThreadPool(2);
  42. messageLock = new ReentrantLock();
  43.  
  44. userTwoConnect = messageLock.newCondition();
  45.  
  46. client = new Client[ 2 ];
  47. }
  48.  
  49. public void execute()
  50. {
  51. try
  52. {
  53. server = new ServerSocket( 22222, 2 );
  54. }
  55. catch( IOException ioException )
  56. {
  57. ioException.printStackTrace();
  58. System.exit(1);
  59. }
  60.  
  61. waitForConnection();
  62.  
  63. messageExchange.execute( client[0] );
  64. messageExchange.execute( client[1] );
  65.  
  66. messageLock.lock();
  67.  
  68. try
  69. {
  70. client[ 0 ].setSuspended(false);
  71. userTwoConnect.signal();
  72. }
  73. finally
  74. {
  75. messageLock.unlock();
  76. }
  77. }
  78.  
  79. public void waitForConnection()
  80. {
  81. messageBuffer = new String();
  82.  
  83. for( int i = 0; i < client.length; i++ )
  84. {
  85. try
  86. {
  87. client[ i ] = new Client( server.accept(), i, messageBuffer ); // waits for connection
  88. messageExchange.execute( client[ i ] );
  89. }
  90. catch( IOException ioException )
  91. {
  92. ioException.printStackTrace();
  93. System.exit(1);
  94. } // end catch
  95. }// end for
  96.  
  97. twoUserConnected = true;
  98. }
  99.  
  100. private void displayInformation( final String info )
  101. {
  102. SwingUtilities.invokeLater(
  103. new Runnable()
  104. {
  105. public void run()
  106. {
  107. serverDisplay.append( "\n" + info );
  108. }
  109. }
  110. );
  111. }
  112.  
  113. private class Client implements Runnable
  114. {
  115. private Socket connection;
  116. private int userNumber;
  117. private ObjectInputStream input;
  118. private ObjectOutputStream output;
  119. private boolean suspended = true;
  120. private String message;
  121. //private Condition canRead;
  122. //private Condition canWrite;
  123.  
  124. public Client( Socket socket, int number, String msg )
  125. {
  126. connection = socket;
  127. userNumber = number;
  128. messageBuffer = msg;
  129. //canRead = messageLock.newCondition();
  130. //canWrite = messageLock.newCondition();
  131.  
  132.  
  133. try
  134. {
  135. input = new ObjectInputStream( connection.getInputStream() );
  136. output = new ObjectOutputStream( connection.getOutputStream() );
  137. }
  138. catch( IOException ioException )
  139. {
  140. ioException.printStackTrace();
  141. System.exit(1);
  142. }
  143. }
  144.  
  145.  
  146. public void run()
  147. {
  148. try
  149. {
  150. displayInformation( "User Number: " + userNumber + " connected\n" );
  151. output.writeObject( "Connection sucessful!\n" );
  152. output.flush();
  153.  
  154. if( !twoUserConnected )
  155. {
  156. if( userNumber == 0 )
  157. {
  158. output.writeObject( "Waiting for correspondent\n" );
  159. output.flush();
  160.  
  161. messageLock.lock(); // wating for second client
  162.  
  163. try
  164. {
  165. while( suspended )
  166. {
  167. userTwoConnect.await();
  168. }
  169. }
  170. catch( InterruptedException exception )
  171. {
  172. exception.printStackTrace();
  173. }
  174. finally
  175. {
  176. messageLock.unlock();
  177. }
  178.  
  179. output.writeObject( "Second user connected!\n");
  180. output.flush();
  181. }
  182. else
  183. {
  184. output.writeObject( "Connection sucessful!\n" );
  185. output.flush();
  186. }
  187. }
  188.  
  189. String msg;
  190.  
  191. while( true )
  192. {
  193.  
  194. if ( input.equals(null) )
  195. {
  196. continue;
  197. }
  198. else
  199. {
  200. messageBuffer = ( String ) input.readObject();
  201. msg = messageBuffer;
  202. displayInformation( messageBuffer );
  203.  
  204. //while ( !userTerminate( message ) )
  205. //{
  206. //messageLock.lock();
  207.  
  208.  
  209.  
  210. output.writeObject ( messageBuffer );
  211. output.flush();
  212. Thread.sleep(10000);
  213.  
  214. }
  215. }
  216. }
  217. catch( ClassNotFoundException exception )
  218. {
  219. exception.printStackTrace();
  220. }
  221. catch( IOException ioException )
  222. {
  223. ioException.printStackTrace();
  224. }
  225. catch( InterruptedException exception )
  226. {
  227. exception.printStackTrace();
  228. }
  229. finally
  230. {
  231. try
  232. {
  233. messageLock.unlock();
  234. connection.close();
  235. }
  236. catch( IOException ioException )
  237. {
  238. ioException.printStackTrace();
  239. System.exit(1);
  240. }// end catch
  241. } // end finally
  242. } // end method run
  243.  
  244. private boolean userTerminate( String msg )
  245. {
  246. if ( msg.equals( "User>>> TERMINATE" ) )
  247. return true;
  248. else
  249. return false;
  250. }
  251.  
  252. public void setSuspended( boolean state )
  253. {
  254. suspended = state;
  255. }
  256. }
  257. }

Main method

  1. import javax.swing.JFrame;
  2.  
  3. public class MorseServerTest
  4. {
  5. public static void main( String args[] )
  6. {
  7. MorseServer application = new MorseServer();
  8. application.setSize(300,300);
  9. application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  10. application.setResizable(false);
  11. application.setVisible(true);
  12. application.execute();
  13. }
  14. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 88
Reputation: knight fyre is an unknown quantity at this point 
Solved Threads: 1
knight fyre's Avatar
knight fyre knight fyre is offline Offline
Junior Poster in Training

Re: Networked chat application between two clients

 
0
  #2
Jun 16th, 2008
A little help would be much appreciated.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 88
Reputation: knight fyre is an unknown quantity at this point 
Solved Threads: 1
knight fyre's Avatar
knight fyre knight fyre is offline Offline
Junior Poster in Training

Re: Networked chat application between two clients

 
0
  #3
Jun 22nd, 2008
...bump
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: Networked chat application between two clients

 
0
  #4
Jun 22nd, 2008
If you don't get any reply after about 5 days, you probably won't get any afterwards. Check out this.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC