Hello all, I have an assignment due on the 14th of May. I have decided to write a chatting system (Server/Client). First I wrote the system using the Threads and DataInputStream/DataOutputStream . Then I wanted to change Input/Output stream into ObjectInputStream/ObjectOutputStream. Now the program is not functioning properly.The problems are; Multiple client connections are not accepting only one connection is accepted at once. For example when one client is connected the second connection is not accepted. But when the first connection gets disconnected, the second connection is accepted. But then an error occurs.
What I expect from the experts is to run the program and understand problem and give me solutions. I would appriciate this very much. The codings are listed bellow. To connect to the server you have to log in to the system. Just enter a user name, that is all what you have to do. Thank you very much for your time..............
SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
Hello ransika. I haven't gone through the GUI construction process. In ChatClient the string ChatName is sent only once. But in ChatServer it is read twice. That means that the ClientThread instance is not created untill the client sends the first message. Again the same happens in ChatClient.run , the same string object is read twice. You need to read exactly what is writen.
In ChatServer the ClientThread is created but not started. Something to correct is reading the name of the client from within the server accepting loop. This is not a problem here because testing is done in the same machine, and the writing of the chatname is done immeditaly after connecting. But in the Internet, once a client has connected no more are accepted untill it arrives its chatname. This could take more time. On the bright side, having set 100 to the queue for pending connections --not the total of connections(*)-- could made up for this; though if you loose connections you know the cause. Try placing all the reading in the client thread. The AWT event-dispacthing thread is responsible for all the drawing and the execution of the event handlers methods. If these methods are given a lengthy task the GUI might freeze. I guess that oOut.writeObject(msgJTA.getText()); in ChatClient.actionPerformed is not very lengthy. And of course it's very convenient. Otherwise another thread would be needed just for the writing. Another thing is having any thread accessing the state of GUI objects after they have been realized (shown). Only the AWT thread should touch GUI objects once they are shown. Otherwise unexpected results might occurr. For instance in ChatClient.run the received string is added to a GUI object from within a thread that is not the event-dispatching thread. The way for any thread to request for the AWT thread to modify the GUI is calling SwingUtilities.invokeLater( Runnable ) . The code within the run method of the Runnable object will be executed by the AWT thread in a thread safe manner.
The ChatServer.listening variable could be set to false to stop the server properly. Say that a button changes its value to finish the Server. For this use it is recommended to declare such variable as volatile. I hope it helps. (*) look at the API for ServerSocket(port, backlog)
SCJP2. Please Indent your code using UBB Code
Joined: Feb 18, 2003
Hello , Thank you very much for the reply. Sure it was something that I never thought about. I will definetly go through what you have said in the reply. Hope it will solve the problems. Thank you very much for your time to answer my question....
subject: Multiple Clients are not accepted + many problems