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.


  • 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 "" even on initial connect. there is an exception thrown, " Connection 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 to be useful to check if the client is still connected the server or was able to find a connection.

    Client c;
    void setup(){
      c = new Client(this,"",5001);
        println("CLIENT DID NOT CONNECT");
        c = null;
    void draw(){
        if(c!=null && !{
           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.