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?