java timeout thread for connection to wrong server

Started by
4 comments, last by frob 9 years, 2 months ago

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.

Advertisement

Connecting to the server blocks execution. The program will wait until that is complete before continuing.

If you create another thread, the other thread shouldn't have the timer; the other thread should have the connection code so the rest of your application, including the UI, can continue to do their things. You can then add a listener to the client so your main threads can get notices when something happens to the connection.

In other words:

1. Main thread handles UI and other interactive stuff. When they connect it launches the secondary thread.

2. Secondary thread in the background will connect and wait for the connection, sending events back through a listener about if the connection succeeds or fails or whatever else.

In other words:

1. Main thread handles UI and other interactive stuff. When they connect it launches the secondary thread.

2. Secondary thread in the background will connect and wait for the connection, sending events back through a listener about if the connection succeeds or fails or whatever else.

The rule when writing code that talks to a server is to always imagine that it will take a long time. 10 seconds at least. You have to write your code to respond to that behavior. You want to disconnect? Connect? Refresh the connection? It will take a long time. In reality, most of the time it will be so fast you don't even see the waiting. But you have to design for all the slow times.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

Don't most good high-level networking libraries provide timeout parameters or other similar constructs for just this purpose? If so, then it might be a good idea to use those rather than spawn a waiter thread just to make the connection non-blocking. Or at least it seems so to me.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

From Google, jMonkey does offer timeout parameters but it has fairly sane defaults. If the OP doesn't like them they can be changed pretty easily.

This topic is closed to new replies.

Advertisement