Processing Month, Day 24 - Client/Server Part 2

Posted by Jan Vantomme on 24 May 2011.
Tags:

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 127.0.0.1 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"127.0.0.1"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 
0;
    
int counter 0;
    for (
int i 3>= 0i--) {
        bits 
|= ((int) bytes[counter] 0xff) << (8);
        
counter++;
    
}
    
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++) {
        xb[i] 
byteBuffer[i];
    
}
    byte[] yb 
= new byte[4];
    for (
int i 04i++) {
        yb[i] 
byteBuffer[i+4];
    
}
    int x 
byteArrayToIntxb );
    
int y byteArrayToIntyb );
    
linexyprevXprevY );
    
ellipsexy1010 );
    
prevX x;
    
prevY y;

Screencast

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

Download

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

Tweet this article