Hello
I have a very simple client-server program that uses sockets simulating a simple atm.
In the client part of the program i have a gui class and another thread to communicate with the server so the gui and the logic are separated and also the gui is not freezing while waiting the thread to communicate with the server.
I create one thread since the clientsocket is created upon connection and lives through the entire session.And the problem is that i have the thread looping in an endless loop and asking the gui if the user pressed any key so it can take care of the actions.
Any suggestions of best practises on this matter?
I don’t want to use RMI since i ‘am developing for educational reasons.
Cheers
You could use a BlockingQueue. The GUI and the client thread would have access to the same queue object. In response to user action, the GUI could put() command objects onto the queue. The client thread will still have a while loop, BUT will use take() to pull command objects off of the queue. take() will block using proper thread synchronization primitives so you won’t have a busy loop. The server response after a client command would likely involve updating some local state and/or updating the gui and I wouldn’t be surprised if SwingWorker got involved.
There are many ways to solve this, but that is the first one that jumped into my head.