Sign in to follow this  
iova

java timeout thread for connection to wrong server

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Edited by Glass_Knife

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this