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 & HelpIntegration › int[][] from Java -> P5
Page Index Toggle Pages: 1
int[][] from Java -> P5 (Read 1265 times)
int[][] from Java -> P5
Jan 21st, 2006, 2:41am
 
I have a Java application that generates an 8x5 matrix every 100-250 ms (it's a server for Eyesweb that does some processing on the data before passing it along). I'd like to send that matrix to a P5 sketch on another mac the easiest way possible.

I've dealt with serializing data to a file before, and I know that its possible to serialize an object and send it across a network connection, but I've never coded this. Can anyone advise if this is a sensible choice? If not, what're my other options..?

Regards,
Ryan Spicer
Re: int[][] from Java -> P5
Reply #1 - Jan 21st, 2006, 5:11am
 
Serializing is a sensible choice if you want something really simple that should always work.

It works by using the DataOutputStream/DataInputStream classes (in java.io I believe), and making some arbitrary 'message' class that implements Serializable.  

Then you can do something like outputstream.write(object) on one end, and Object o = inputstream.read() on the other end.

Just build that object with whatever members you want to send across (such as your matrix), setup the streams (with sockets and socketserver), then do the appropriate casting, try/catch blocks and be on your way.

Your other choice is to use some other input/output stream method and handle data read/write yourself, which is probably not worth the effort for however many bytes you may send.  If it's in a local network, you wouldn't even notice at 4~10 times per second.

Marcello
Re: int[][] from Java -> P5
Reply #2 - Jan 23rd, 2006, 3:07am
 
Can I ask for some more help getting this implemented?

I've got a server that listens to a port, then accepts a connection from a client, which connects to it..

I want the server to send my int[][] to the client each time a new int[][] is available. That behavior works.

The problem is that my client starts checking too early (before there are int[][]s to read) and throws a nullpointer exception.

Is there any easy way to just loop myObject = myObjectReader.readObject() without any complex protocol business? my current code is

...
while (true) {
Object temp = myObjectReader.readObject();
if (temp != null)
  myIntMatrix = (int[][]) temp;
}
...

and the null pointer exception is thrown the very first time the readObject() method is called.

Right now the server's behavior is "don't send anything to the established connection until the first int[][] is available, then send a new int[][] every time one appears."

Any thoughts?

Ryan
Re: int[][] from Java -> P5
Reply #3 - Jan 23rd, 2006, 3:23pm
 
that actually sounds like myObjectReader itself is null, or that something inside myObjectReader is null (connection no good or something like that). because otherwise, your code should be fine.
Re: int[][] from Java -> P5
Reply #4 - Jan 24th, 2006, 7:04am
 
Thanks Fry. I'll give the code another once-over with a fine-toothed comb and make sure the error's really where I think it is.
Re: int[][] from Java -> P5
Reply #5 - Jan 25th, 2006, 4:39am
 
You were right, Fry. It looks like I forgot to instantiate the object reader.

Now that I am instantiating the object reader, my program hangs on that line.

Here's the source:

import java.net.*;
import java.io.*;

public class TelemetryClient {
private SocketmySocket;
private BufferedInputStream myBIS;
private ObjectInputStream myOR;

private CubbyHole myCH;

private boolean debug = true;

public static void main(String[] args) {
new TelemetryClient("localhost");
}

TelemetryClient(String IP) {

myCH = new CubbyHole();
try {
mySocket = new Socket(IP, 4444);
myBIS = new BufferedInputStream(mySocket.getInputStream());
println("buffered input stream ok");
myOR  = new ObjectInputStream(myBIS);
println("object input stream ok");
} catch (UnknownHostException e) {
System.err.println("Error!");
System.exit(1);
} catch (IOException e ) {
System.err.println("IOException!");
System.exit(1);
}

new ReceiveThread().start();
}

public CubbyHole getCubbyHole() {
return myCH;
}

class ReceiveThread extends Thread {
public void run() {
while(true) {
try {
int[][] data = (int[][]) myOR.readObject();
myCH.put(data);
if (data[3][3] == -1234)
break;
} catch (ClassNotFoundException e) {
System.err.println("Unknown Class Exception!");
} catch (IOException e) {
System.err.println("IOException!");
}
}

}
}

private void println(String message) {
if (debug)
System.out.println("Client says: " + message);
}

}

the problem seems to be with the line "myOR  = new ObjectInputStream(myBIS);" because the println() immediately above prints, and immediately below fails. It's not throwing an exception, just freezing..

I'm sorry for asking all the silly questions but I haven't worked with sockets before, and I'm in a wee bit over my head..

Ryan
Page Index Toggle Pages: 1