How auto reconnect a Client if the Server is not present or lost?

edited November 2013 in How To...

I'm trying to create an auto reconnect to a client. I am using the library already present, that net. when you try to instantiate a client and the server is not yet present, there is of course an exception error and everything stops. I have tried in various ways, but I can not control a specific exception, with the processing net library?. so ... How to do to have a client who tries to reconnect if a server is not yet present, or the connection is lost? thanks people.

Answers

  • The exception is just a println() from the library, it isn't really thrown to the sketch, so you cannot catch it. Processing rarely throws exceptions, in general, except when it exposes low level functions, precisely to allow finer control.

    So, the only way to check if there is an error is to check the returned value, to see if it is null.

  • Can you elaborate on that? which value are we to look for as null?

    even when the server is stopped, println(myclient) gives something like "processing.net.Client@21f33544" even on initial connect. there is an exception thrown, "java.net.ConnectException: Connection refusedjava.net.ConnectException: Connection refused". calling myclient = new Client(...) a 2nd time, once the server is running, still results in the same handle but a new exception "java.lang.NullPointerException". calling the stop() method first also does not seem to allow reconnecting. from the outside, it appears that there's a bug in the Client library where it is failing to release resources so that a re-connection is possible.

  • edited March 2014

    I have actually encountered the same problem when working on my project and I found the client.active() to be useful to check if the client is still connected the server or was able to find a connection.

    import processing.net.*;
    Client c;
    void setup(){
      c = new Client(this,"127.0.0.1",5001);
      c.write("O__O");
      if(!c.active()){
        println("CLIENT DID NOT CONNECT");
        c = null;
      }
    }
    
    void draw(){
        if(c!=null && !c.active()){
           println("CLIENT GOT DISCONNECTED");
           c = null;
        }
    }
    

    I'm not really sure if this is a good way to handle these problems but it works for me.

Sign In or Register to comment.