Processing Month, Day 24 - Client/Server Part 2

Posted by Jan Vantomme on 24 May 2011.

In part two of Client/Server programming with Processing, we’ll take a look at creating the client application. You need to create a new client object with the same port number as the server. If you run the client sketch on the same machine, you can use for the IP Address, if it runs on another computer, you need to use the IP Address of the machine that runs the server sketch.

client = new Clientthis""5000 ); 

We also need a function to convert the byte array back to an integer. You can find this one in the functions tab.

int byteArrayToInt(byte[] bytes{
    int bits 
int counter 0;
    for (
int i 3>= 0i--) {
|= ((int) bytes[counter] 0xff) << (8);
return bits;

The final thing we need to do is to read the data the server application is sending with clients.readBytesUntil() into a bytebuffer and get the x and y coordinates out of there for drawing ellipses and lines to the screen.

if ( client.available() > {
    int byteCount 
client.readBytesUntilinterestingbyteBuffer );
byte[] xb = new byte[4];
    for (
int i 04i++) {
    byte[] yb 
= new byte[4];
    for (
int i 04i++) {
    int x 
byteArrayToIntxb );
int y byteArrayToIntyb );
linexyprevXprevY );
ellipsexy1010 );
prevX x;
prevY y;


I’ve created a screencast so you can see what the application does. You can find it on Vimeo.


Download the client sketch you’ve read about today. I’ve included the server sketch from yesterday.

Tweet this article