We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › try-catch for socket exception
Page Index Toggle Pages: 1
try-catch for socket exception (Read 2070 times)
try-catch for socket exception
Dec 17th, 2009, 2:11am
 
Hello

I'm using Processing to controll Arduino board installed in LinkSys routerbot. Sometimes robot is down (for example battarey is discharged) and I want Processing application to correctly handle socket exception. But 2 days searching in google and some 20 different trys with try-catch yelded no results. Socket and IOExceptions are never cought. Is it possible in Processing? How can I display message that target host is down?


java.net.ConnectException: Connection timed out: connect
     at java.net.PlainSocketImpl.socketConnect(Native Method)
     at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
     at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
     at java.net.Socket.connect(Socket.java:525)
     at java.net.Socket.connect(Socket.java:475)
     at java.net.Socket.<init>(Socket.java:372)
     at java.net.Socket.<init>(Socket.java:186)
     at processing.net.Client.<init>(Client.java:60)
Re: try-catch for socket exception
Reply #1 - Dec 17th, 2009, 2:29am
 
Yes, Processing tries to make things easier for non-technical people by catching most exceptions and displaying an error, so people don't have to put try/catch stuff everywhere or wonder what is this strange error message about uncaught exception (seen a number of such cases on the forum...).
If you look at Client.java source, you will see that nearly all the Client constructor is surrounded by a try/catch which just dumps the stack trace.
A bit annoying for a finer control...

Now, at least, you can check the active() status after creating the client: if it returns false, you know your client haven't been properly initialized (I think -- not tested...).
Re: try-catch for socket exception
Reply #2 - Dec 18th, 2009, 9:29am
 
PhilLho

Thank you for your clear answer and time!

The sollution was to wait in the loop until byte is received from server. Server was instructed to issue welcome message on connecting of client. In slightly unusuall way code is working now.
Re: try-catch for socket exception
Reply #3 - Dec 21st, 2009, 7:49am
 
hy,


can you make a code example?

i'm working with the SharedCanvasServer example. this works great.

the only thing is, if the client starts earlier than the server, it would give me this error:



java.net.ConnectException: Connection refused
     at java.net.PlainSocketImpl.socketConnect(Native Method)
     at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
     at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:433)
     at java.net.Socket.connect(Socket.java:524)
     at java.net.Socket.connect(Socket.java:474)
     at java.net.Socket.<init>(Socket.java:371)
     at java.net.Socket.<init>(Socket.java:184)
     at processing.net.Client.<init>(Client.java:60)
     at SharedCanvasClient.setup(SharedCanvasClient.java:43)
     at processing.core.PApplet.handleDraw(PApplet.java:1402)
     at processing.core.PApplet.run(PApplet.java:1327)
     at java.lang.Thread.run(Thread.java:613)




so i wondered, if there is a workaround, so the app doesnt crash.

is try/catch a way? what is the exception I have to set after catch(***)



sorry, I'm not the most talented programmer switzerland has ever seen.  Wink


thank you for the help!
Re: try-catch for socket exception
Reply #4 - Dec 21st, 2009, 8:19am
 
hi nbuechi, your client sketch actually doesnt crash. an exception is thrown from within the Client class (PhiLho explains this in reply #1) which does not affect your sketch but doesnt connect to a server either. if you run the SharedCanvasClient without being able to connect to a server, you will still be able to draw inside the display window. to prevent your app from throwing further nullpointer exceptions, use the Client's active() method to check if your client is connected and active, or not. if the client is not active (c.active() returns false), dont write to the server.
to connect to a server at a later time while your sketch is running, you could use a key event to retry connecting to your server.
Page Index Toggle Pages: 1