Concurrency Problem in Java
I am developing a client server chat application in Java. It is a secure application where messaging is done using cryptographic algorithms. I have one server and it can support many clients. My problem is when one client logs into the server it works fine, but when another user logs in the server starts giving me wrong cipher text paddings.
I can't figure out the problem, according to my logic, when a new connection request is made to the server, the server creates a thread to listen to the client. Is it possible that after instantiating the thread class, it handles all the processing for the first client but not the second client, because the variables in the server listener thread class already have some previous value, and thus the cipher text is not decrypted properly?
Please advise how I can make this process more reliable so that the number of clients does not affect how well the server is performing.
Hi, The code looks like this:
When the server starts:
Socket in= serverSocket.accept();
Receive rlt = new Receive(in);
Thread receiveReq = new Thread(rlt);
receiveLoginReq.start();
the receiving thread now waits for an incoming message and executes the process according to the message type. When multiple clients are called, the server is working fine, the problem starts when one client exits and then tries to reconnect again. The server always gives an error in the following pattern:
- First time HAsh does not match error for second client
- Second time javax.crypto.BadPaddingException: The given final block is incorrectly corrected by error
When this happens, I need to restart the server and restart both clients, only then both clients work. but again if one client ends the connection and tries to connect again, the same 2 errors happen the same. and then restart the server again.
Any advisor would be much appreciated. Thanks to
source to share
I can only guess the reasons without seeing the complete source code. I am assuming you are using CipherInput / OutputStream for your encryption. You have to use separate Cipher instances for each stream (each I / OStream). Every time you create a new connection or reconnect, run the init Cipher method on the client and server side and create new instances of CipherInput / OutputStream.
Cryptographic objects are stateful, so they cannot be shared between threads. Each thread and each connection must have its own separate set of stateful objects.
source to share