This is part of a messenger project in java. Because clients use direct connections to chat, I want eavry client to listen on some port, and others to make a socket to that address. but when i call ServerSocket.accept() in another thread it appears that all threads have been suspended. which means nothings happens after executing that command. Here is the code which makes new thread.
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
while(true){
System.out.println("flag1");
Socket socket = listeningSocket.accept();
System.out.println("flag2");
new Chat(socket).setVisible(true);;
jTextArea1.append("successfully connected\n");
}
} catch (NullPointerException e) {
System.out.println("i know");
}
catch (IOException e) {
e.printStackTrace();
jTextArea1.append("error in recieving connection\n");
}
}
});
any ideas how to solve this?
Appears how? accept() only blocks the current thread. Are you calling it in the AWT thread? e.g. an actionPerformed() method? Don’t do any network operations in those methods, use separate threads.