Concurrency Problem in Java 
<p>I am designing a client-server chat application in Java. This is a secure application where the messages are exchanged using cryptographic algorithms. I have one server and it can support many clients. My problem is that when one client logs on the server it works fine, but when another user logs into the system, the server starts giving me bad padding exceptions for the encrypted text. </p>

<p>I am not able to figure out the problem, according to my logic, when new connection request to server is made, the server creates a thread for listening to the client. Is it possible that once the instance of thread class is created, it does all the processing correctly for the first client, but not for the second client because the variables in server listener thread class already have some previous value, and thus the encrypted text is not decrypted properly?</p>

<p>Please advise how I can make this process more robust so that the number of clients does not affect how well the server functions.</p>

<p>Hi, The code is like this :</p>

<p>When Server Starts:</p>

<pre><code>Socket in= serverSocket.accept();

                Receive rlt = new Receive(in);
                Thread receiveReq = new Thread(rlt);
                receiveLoginReq.start();
</code></pre>

<p>now the Receive Thread waits for the incoming message and do the process according to message type. When more than one client is invoked, Server works fine, problem starts when one client terminates and then again tries to reconnect. Server always gives the Error in following pattern:</p>

<ol>
<li>First time the HAsh not matched error for second client</li>
<li>Second time javax.crypto.BadPaddingException: Given final block not properly padded error</li>
</ol>

<p>When this happens, I need to restart server and restart both clients, only then both clients works. but again if one client terminates connection and again tries to reconnects, the same 2 errors occurs in the same manner. and then again restart Server. </p>

<p>Any Advise will be highly appreciated. 
Thanks</p>
