I want to connect to a Game-Server with an IP that is put in by the user. As long as the server is the right one or the Server don't exist everything is fine, I got a connection or an Exception.
The Problem is, if I put in an IP to an existing Server that don't run my Game. In this case the programm stays in the connection-Event and don't react to anything. I thought about setting up a timer-Thread but that didn't work. The Programm don't react on any input by the user and don't even will be closed.
I implemented the Client as a Thread and the Timer as a Thread. The Timer-Thread should stop the Client-Thread if it's out of time.
My Timer:
public class Timer extends Thread{
private int counter;
private Coplayer client;
Timer(int i, Coplayer c){
if (i>=0) {
counter = i;
} else {
counter = -i;
}
client = c;
}
@Override
public void run (){
if (counter>0) {
counter -= 1;
} else {
client.stop();
}
}
and my Client:
try {
Timer t1 = new Timer(100, this);
client=Network.connectToServer(ip,port);//this is the line the programm halts
t1.stop();
client.start();
connected = true;
Serializer.registerClass(Message_String.class);
Serializer.registerClass(Message_Player.class);
CL = new ClientListener();
client.addMessageListener(CL, Message_String.class);
client.addMessageListener(CL, Message_Player.class);
return "Verbunden.";
} catch (java.net.ConnectException e){
connected = false;
return "Verbindung konnte nicht hergestellt werden.";
} catch (java.net.UnknownHostException e){
connected = false;
return "Server nicht bekannt.";
} catch (IOException ex) {
connected = false;
return "Falsche Eingabe.";
}
I am using JAVA with Jmonkey, if that matters.