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 › Server/Client Issue
Page Index Toggle Pages: 1
Server/Client Issue (Read 728 times)
Server/Client Issue
Dec 15th, 2009, 1:57pm
 
Hello,

I am trying to use the server/client class to receive data from a webpage. Basically, I send an AJAX call from the webpage, and I use processing to parse out the information I need (its a midi note that I am sending). It works great, except that it only lets me send about 6 notes, and then it crashes saying:

"Exception in thread "Animation Thread" java.lang.StringIndexOutOfBoundsException: String index out of range: -3"

Here is my code:

Code:

import processing.net.*;
import themidibus.*; //Import the library

MidiBus myBus; // The MidiBus


int port = 10002;
boolean myServerRunning = true;
int bgColor = 0;
int direction = 1;
int textLine = 60;


Server myServer;

void setup() {
 size(800,800);
 background(0);
 textFont(createFont("SanSerif", 16));
 myServer = new Server(this, port); // Starts a myServer on port 10002

 MidiBus.list(); // List all available Midi devices on STDOUT. This will show each device's index and name.
 myBus = new MidiBus(this, 2, 10); // Create a new MidiBus with no input device and the default Java Sound Synthesizer as the output device.
}

void draw() {
 int channel = 0;
 int pitch = 64;
 int velocity = 127;
 int number = 0;
 int value = 90;
 if (myServerRunning == true)
 {
   text("server", 15, 45);
   Client thisClient = myServer.available();
   if (thisClient != null) {
     if (thisClient.available() > 0) {
       String content = thisClient.readString();
       int a = content.indexOf("/?");
       int b = content.indexOf("HTTP");
       String c = content.substring(a + 2, b - 1);
       println(content.substring(a + 2, b));
       int i = parseInt(c);
       myBus.sendNoteOn(channel, i, velocity); // Send a Midi noteOn
       delay(200);
       myBus.sendNoteOff(channel, i, velocity); // Send a Midi nodeOff
       text("mesage from: " + i, 15, textLine);
       textLine = textLine + 35;
       thisClient.stop();
     }
   }
 }

}



Any help would be appreciated. Thanks.

-Thomas
Re: Server/Client Issue
Reply #1 - Dec 15th, 2009, 2:02pm
 
As usual, when reporting an exception, please give the full stack trace, or at least the few lines below the message: it indicates where the problem is (line number, method, class...).

Quote:
I send an AJAX call

Are you using JavaScript?

The key problem is probably within the indexOf calls: you should check the result, it is -1 if the string isn't found.
Re: Server/Client Issue
Reply #2 - Dec 15th, 2009, 2:21pm
 
Heres the entire error:

Exception in thread "Animation Thread" java.lang.StringIndexOutOfBoundsException: String index out of range: -3
     at java.lang.String.substring(Unknown Source)
     at midiserver.draw(midiserver.java:92)
     at processing.core.PApplet.handleDraw(PApplet.java:1423)
     at processing.core.PApplet.run(PApplet.java:1328)
     at java.lang.Thread.run(Unknown Source)

Yes, I am using Javascript from the webpage to send the AJAX call (Asynchronous Javascript and XML).

It actually works fine until it freaks out, and it won't let me check the result... as in, it gives me that error when I try to check the result.

Thanks for your help.

-Thomas
Re: Server/Client Issue
Reply #3 - Dec 15th, 2009, 8:23pm
 
Thanks! I got it working, I just put an exception in if the indexOf equals -1. Thanks so much.

-Thomas
Page Index Toggle Pages: 1