Capturing Net Client Error if Server Not Running

Hi, I’m trying to program a client application based on the SharedCanvasClient example. My current task is to handle the case where the server might not be running. When the server example is not running and I run the client example, it responds with:

java.net.ConnectException: Connection refused: connect

So, I tried the try/catch commands:

  try{
  c = new Client(this, "127.0.0.1", 12345); // Replace with your server's IP and port
  } catch (java.net.ConnectException e){
    println("caught it");
  }

it responds with:

Unreachable catch block for ConnectException. This exception is never thrown from the try statement body

My goal is to retry, or put a status message up on the screen of the client program if the server can not be reached.

Can someone point me in the right direction to address this?

thank you

That’s more a workaround, but it happened to me and i used the simple class Exception for it.

Hi, thank you for the reply!

did you mean this:?

  try{
  c = new Client(this, "127.0.0.1", 12345); // Replace with your server's IP and port
  } catch (Exception e){
    println("caught it");
  }

this gives me the original error I reported.

Or did you mean something else? I’m new to processing/java/etc. so I could be missing something obvious

I think the problem is that the Client Constructor starts a new thread, but I have no idea, how to catch exceptions in a thread. Processing tries to follow the exception in your code, and if threads point outside of your sketchfolder, just your “thread caller” ist highlighted.
Python for example does not matter if it is in your code, and a looong exception is shown to the mattering script.

Thanks again for your input! It sort of makes sense to me…
I’ll do some more research and if I find a solution I will post.

1 Like

Hello,

Try this:

Modified SharedCanvasClient
/**
 * Shared Drawing Canvas (Client) 
 * by Alexander R. Galloway. 
 * 
 * The Processing Client class is instantiated by specifying a remote 
 * address and port number to which the socket connection should be made. 
 * Once the connection is made, the client may read (or write) data to the server.
 * Before running this program, start the Shared Drawing Canvas (Server) program.
 */


import processing.net.*;

Client c;
String input;
int data[];

boolean clientConnected = false;

void setup() 
{
  size(450, 255);
  background(204);
  stroke(0);
  frameRate(5); // Slow it down a little
  // Connect to the server's IP address and port
  while(!clientConnected)
    {
    c = new Client(this, "127.0.0.1", 12345); // Replace with your server's IP and port
    if(!c.active())
      {
      println("Not connected to server");
      println("Start the server");
      clientConnected = false;
      
      }
    else
      {
      println("Connected to server");
      clientConnected = true;
      }
    }
  }

void draw() 
  {
  if (clientConnected)
    {
    // Do something  
    }
  else
    {
    // Do something else
    }
  }

See this topic:

:slight_smile:

1 Like

Hi glv!
Thank you so much for the solution to my problem!

Kind of embarrassing, I looked through the net library reference and did not notice active().

cheers,