06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

368 <strong>Learning</strong> <strong>Processing</strong><br />

Let’s assume we want <strong>to</strong> send the number 42. We have two options:<br />

client.write(42); // sending the byte 42<br />

In the line above, we are really sending the actual byte 42.<br />

client.write( " 42 " ); // sending the String " 42 "<br />

In the line above, we are sending a String . Th at String is made up of two characters, a ‘ 4 ’ and a ‘ 2 ’ .<br />

We are sending two bytes! Th ose bytes are determined via the ASCII (American Standard Code<br />

for Information Interchange) code, a standardized means for encoding characters. Th e character ‘ A ’<br />

is byte 65, the character ‘ B ’ 66, and so on. Th e character ‘ 4 ’ is byte 52 and ‘ 2 ’ is 50.<br />

When we read the data, it is up <strong>to</strong> us <strong>to</strong> know whether we want <strong>to</strong> interpret the bytes coming in<br />

as literal numeric values or as ASCII codes for characters. We accomplish this by choosing the<br />

appropriate read( ) function.<br />

int val � client.read(); // matches up with client.write(42);<br />

String s � client.readString(); // matches up with client.write( " 42 " );<br />

int num � int(s); // convert the String that is read in<strong>to</strong> a number<br />

We are now ready <strong>to</strong> create a server <strong>to</strong> receive the messages from the client. It will be the client’s job <strong>to</strong><br />

format those messages with our pro<strong>to</strong>col. Th e job of the server remains simple: (1) receive the data and<br />

(2) relay the data. Th is is similar <strong>to</strong> the approach we <strong>to</strong>ok in Section 19.2.<br />

Step 1 . Receiving data.<br />

Client client = server.available();<br />

if (client ! = null) {<br />

incomingMessage = client. readStringUntil( ' * ' );<br />

}<br />

What is new in this example is the function readStringUntil( ) . Th e readStringUntil( ) function takes<br />

one argument, a character. Th at character is used <strong>to</strong> mark the end of the incoming data. We are simply<br />

following the pro<strong>to</strong>col established during sending. We are able <strong>to</strong> do this because we are designing both<br />

the server and the client.<br />

Once that data is read, we are ready <strong>to</strong> add:<br />

Step 2. Relaying data back out <strong>to</strong> clients.<br />

Client client = server.available();<br />

if (client ! = null) {<br />

incomingMessage = client.readStringUntil( ' * ' );<br />

server.write(incomingMessage);<br />

}<br />

Because we designed our own<br />

pro<strong>to</strong>col and are not using<br />

newline/carriage return <strong>to</strong> mark<br />

the end of our message, we must<br />

use readStringUntil() instead of<br />

readString().<br />

Writing the message back out <strong>to</strong><br />

all clients.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!