I want a Frame to be displayed while my Program connects with other Players. But even if the Frame is running in his own Thread it is still freezing while the Serversocket is trying to connect. How can I prevent it from freezing?
public static boolean connectH(String check, String data, int k){
ServerSocket serverSocket = null;
Socket clientSocket = null;
PrintWriter out = null;
BufferedReader in = null;
message = "waiting for someone to connect to your " + check + "-game";
th = new Thread(){
@Override
public void run() {
c = new Canceler(message);
}
};
th.start();
try {
serverSocket = new ServerSocket(Integer.parseInt(data));
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Could not listen on port: " + data);
Main.m.finishgame(-1);
}
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
try {
out = new PrintWriter(clientSocket.getOutputStream(), true);
} catch (IOException e) {
e.printStackTrace();
}
try {
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
try {
if (in.readLine().equals(check)){
out.println(k);
c.remove();
return true;
}else{
out.println(String.valueOf(-1));
c.remove();
return false;
}
} catch (IOException e) {
e.printStackTrace();
}
c.remove();
return false;
}