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 & HelpOther Libraries › Network - Client SocketException handling
Page Index Toggle Pages: 1
Network - Client SocketException handling (Read 965 times)
Network - Client SocketException handling
Nov 9th, 2009, 1:21pm
 
Hey everybody,

I have a problem with the Network library. I have a web page which returns a string for use in Processing, like this: <begin>[thestring]<end>. I use the Network library and a Client object to do this. It works fine, until the connection is (temporarily) lost, for example by the server restarting, but it happens once every 10 minutes for some reason. The Client throws an exception on this; from the Network Library Reference:
Quote:
A client connects to a server and sends data back and forth. If anything goes wrong with the connection, for example the host is not there or is listening on a different port, an exception is thrown.


My problem is that I don't know how to handle the exception.. I tried adding try/catch routines at various points, but it just keeps coming up with the exception stuff. I also tried catch(SocketException e), since that is the exception that is thrown, according to Processing, but it is nowhere allowed ("nothing inside throw can raise this exception").
Code:
/**
* HTTP Client.
*
* Starts a network client that connects to a server on port 80,
* sends an HTTP 1.1 GET request, and prints the results.
*/

import processing.net.*;

Client c;

String data;
String prevData;
PFont font;
int prevSec = 0;

void mpcRequest(){
 println("HTTP req.");
 c.clear();
 c.write("GET {MYSCRIPT} HTTP/1.1\n"); // Use the HTTP "GET" command to ask for a Web page
}


void setup() {
 frameRate(25);
 size(600, 200, P2D);
 background(50);
 fill(200);
 //load font
 font = loadFont("Verdana-16.vlw");
 textFont(font);
 // Draw text more accurately and efficiently.
 textMode(SCREEN);
 textAlign(LEFT,TOP);

 c = new Client(this, "MY_HOST", 80); // Connect to server on port 80
 prevData = "init";
}

void draw() {
 try{
   if (c.available() > 0) { // If there's incoming data from the client...
     //wait a sec before trying to read it
     delay(100);
     data = c.readString(); // ...then grab it and print it
     println(data);
     if(data.indexOf("<begin>")!=-1&&data.indexOf("<end>")!=-1){
       data = data.substring(data.indexOf("<begin>")+7,data.indexOf("<end>"));
     }
     else {
       data = "Invalid data received";
     }
     if(data.equals(prevData) != true){
       //only update stuff if there is anything new
       background(50);
       text(data, 10, 10);
       delay(10);
       prevData = data;
     }
   }
   if(second()-prevSec>9||(second()<prevSec&&second()>(prevSec-51))){
     //10 seconds elapsed: new request
     mpcRequest();

     prevSec = second();
   }
 }
 catch(Exception e){
   println("EXCEPTION");
 }
}


And this is the error I get (in the "console window" of Processing):
Quote:
22.17:32 => HTTP req.
HTTP/1.1 200 OK
Date: Mon, 09 Nov 2009 21:17:35 GMT
Server: Apache/2.2.14 (Debian)
X-Powered-By: PHP/5.2.11-1
Content-Length: 38
Content-Type: text/html
<begin>[MY_STRING]<end>

22.17:42 => HTTP req.
22.17:52 => HTTP req.

java.net.SocketException: Software caused connection abort: socket write error
     at java.net.SocketOutputStream.socketWrite0(Native Method)
     at java.net.SocketOutputStream.socketWrite(Unknown Source)
     at java.net.SocketOutputStream.write(Unknown Source)
     at processing.net.Client.write(Client.java:442)
     at processing.net.Client.write(Client.java:468)
     at HTTPClient_no_Arduino.mpcRequest(HTTPClient_no_Arduino.java:49)
     at HTTPClient_no_Arduino.draw(HTTPClient_no_Arduino.java:95)
     at processing.core.PApplet.handleDraw(PApplet.java:1423)
     at processing.core.PApplet.run(PApplet.java:1328)
     at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
     at processing.net.Client.write(Client.java:442)
     at processing.net.Client.write(Client.java:468)
     at HTTPClient_no_Arduino.mpcRequest(HTTPClient_no_Arduino.java:50)
     at HTTPClient_no_Arduino.draw(HTTPClient_no_Arduino.java:95)
     at processing.core.PApplet.handleDraw(PApplet.java:1423)
     at processing.core.PApplet.run(PApplet.java:1328)
     at java.lang.Thread.run(Unknown Source)


The connection was lost after the first (succesful) request. The second one never finishes? and the third one crashes. Can anyone help me with this?
Re: Network - Client SocketException handling
Reply #1 - Nov 10th, 2009, 2:33am
 
The problem is that Processing tries, as much as possible, to insulate users from exceptions, so most of the time it handles them itself.
In Client.java, we can see the following code:
Code:
  public void write(byte bytes[]) {
try {
output.write(bytes);
output.flush(); // hmm, not sure if a good idea
} catch (Exception e) { // null pointer or serial port dead
e.printStackTrace();
stop();
}
}

ie. it catches the exception, dumps it and stops the client.
output is public, so you can do the above yourself, thus controlling exception handling.
Re: Network - Client SocketException handling
Reply #2 - Nov 10th, 2009, 6:34am
 
Ah, that explains. I've substituted the c.output.write for the c.write function in mpcRequest(), and done the error catching there. Now it works (or at least it can run for 2 hours with no errors) Smiley Thanks for your help!
Page Index Toggle Pages: 1