Sending byte array over processing network (Processing 3)

Oh, my mistake, I read your code again and I didn’t notice you are sending the data as jpg, which will make it a bit more difficult, because each image you send will have a different amount of bytes. I will mention it later.

If I understand it correctly, you have two computers and you want to send video from both of them to the other one.

I see several problems in your code. What I would do is start with a new sketch and try to build it up in several steps. Don’t go to the next step until the previous step works reliably. This way you can learn how each part works and focus on one problem at a time.

1. Making the connection

In the server sketch, you first create a Server and then wait for the client to connect. You would use serverEvent() to get notified when a Client connects. You would save the client in a global variable (like Client c you have there). You can use disconnectEvent() to get notified when the client disconnects and set the reference back to null. In your draw() you write or read bytes only if client c is connected (not null).

In the client sketch, just create Client like you do now. If the server is running, it should connect. If the server is not running, the client will not work and you will need to restart the sketch, or try to create a new client. You can use c.active() to check whether the connection was made or not. If it returns false, you need to create a new client.

2. Sending data of fixed size

Try to send a random int from the client to the server every frame and print them out on the server when they arrive. You would use a similar code as in my previous post, but the size of one message is now 4 bytes (size of int). Try sending ints larger than 255 or less than 0 to check if all four bytes are sent and received correctly.

On the client, you can use

byte[] bytes = java.nio.ByteBuffer.allocate(4).putInt(yourRandomValue).array();
c.write(bytes);

ByteBuffer is like a byte array, but has some useful methods which allow you to put there other data types and get them as a byte array. To optimize, you can create the buffer in setup() and reuse it.

On the server, you would wait until there are at least 4 bytes available, then read them and convert them back to int by using ByteBuffer again:

if (c != null) {
  while (c.available() >= 4) {
    byte[] bytes = c.read(4);
    int i = java.nio.ByteBuffer.wrap(bytes).getInt();
    println("Int arrived:", i);
  }
}

Try sending more ints every frame to check that your code reads all of them.

3. Sending data of variable size

To send messages of variable lengths (like your jpgs), you need to tell the other side how many bytes to expect in each message. Otherwise it won’t be possible to tell where one message ends and the next one begins. You can do this by first sending an int with the number of bytes to expect, then you send the bytes.

[int with size of message A][message A][int with size of message B][message B]...

On the receiving end (server), you wait until there is 4 bytes available (one int), you read the int which tells you the size of the message, then you wait until the whole message arrives, and finally you read it and process it. Then you wait for another int which will tell you the size of the next message. I can help more with this after you make part 2 work.

Give this a try and let us know if you get stuck. Just try to keep it as simple as possible and improve it step by step. You can add video and bidirectional communication once you have this working :wink:

3 Likes