Reputation: 523
so i've been trying to learn about sockets, and was trying to create a simple chat app where i have 2 JFrames
and each connects to socket, the problem is that every time i run the JFrames
the frames start correctly, but when i click on the connect_button
the socket starts connection but doesn't let me continue with the operations, everything is blocked from this point forward, i've been realy trying to understand but im stuck here, any thoughts ?
i think my problem is that the sockets should go in their own Threads, but i've seen other solutions and seen that they don't implement Threads, or at least in the code that they show.
maybe my socket connection is implemented incorrectly.
¿Is it wrong to use PrintStream
if so, is it still posible to use this class to proceed with this excercise and which Stream
class should i use ? Taking into count that i understand that bufferedStreams are the way to go if you want efficiency
Upvotes: 0
Views: 527
Reputation: 41200
If you retain your GUI thread to make socket communication it is for sure, this will block your thread(UI).
What is required is some async or non blocking way to make socket communication, which does not block your GUI thread.
Keep your GUI thread separate and runs on its own and make socket connection through creating FutureTask
or ForkJoinTask
or Thread
or non blocking socket communication which updates once you received communication message.
Example to create non blocking socket
Upvotes: 0
Reputation: 347214
Swing is not thread safe. That is, all UI events occur within the context of the Event Dispatching Thread, including paint events.
Anything that blocks this thread will prevent the EDT from processing new events and essentially make your application look like it's hung.
Take a look at Concurrency in Swing for more details...
The call to initServerConnector
in the actionPerformed
method of the Messenger_Controller
class is creating a new Conector
which is creating a ServerSocket
and calling accept
on it, which is a blocking method. This is preventing the EDT from processing new events until it returns.
Instead, you should be using either a SwingWorker
or Thread
to manage the Socket
connections and communications.
See Worker Threads and SwingWorker for more details.
Swing components should also not be updated from outside the context of the EDT. This means if you need to make changes to the UI while processing the socket comms, you will need to sync those updates back to the UI.
If you're using a SwingWorker
, you can use the publish
/process
methods or you will need to use SwingUtilities.invokeLater
, both approaches will sync the calls back to the EDT where you can makes changes safely...
Upvotes: 1
Reputation: 417662
You create your socket and handle network traffic in the EDT (Event Dispatching Thread) because you do this in an ActionListener
. This is the same thread that is used to paint the GUI of your application and handle interactions with the user.
You should not do lengthy task in the EDT but start a new thread for them.
Upvotes: 0