I've got a problem.
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 doesn't exist everything is fine, I've got a connection or an Exception.
The Problem is, if I put in an IP to an existing Server which doesn't run my Game. In this case the program stays in the connection-Event and doesn't react to anything. I thought about setting up a timer-Thread although that didn't work. The Program doesn't react on any input by the user and only allows you to close it.
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.