0

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.

halfbit
  • 3,414
  • 1
  • 20
  • 26
iova
  • 3
  • 4
  • Where is the Network.connectToServer method defined? Such methods should have a timeout argument. – lbalazscs Jan 27 '15 at 18:42
  • It's a Method from spidermonkey. I havn't found any informations if it has a timeout, but it calls the exceptions i am catching. But on wrong Server there is no exception, but no connection, too. I can specify the game, the server shall running, but that don't help either but nothing works than. – iova Jan 27 '15 at 19:44
  • for more information have a look in my troubleshooting on http://hub.jmonkeyengine.org/t/check-server-game-id/31342/3 but there we didn't find a solution also, that's why I'm asking you. – iova Jan 27 '15 at 19:46

1 Answers1

1

The timeout issue was already discussed: http://hub.jmonkeyengine.org/t/set-timeout-for-network-connecttoserver/30836

I still think that the jmonkeyengine developers should create a version of Network.connectToServer which has a timeout parameter. It should not be hard, because java.net.Socket has timeout support. (maybe you could ask them)

Anyway, as a workararound you can always start the connection attempt on a separate thread like described here: How to timeout a thread

Community
  • 1
  • 1
lbalazscs
  • 17,474
  • 7
  • 42
  • 50