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.

19.4<br />

void draw() {<br />

background(255);<br />

// Display message from server<br />

fill(newMessageColor);<br />

textFont(f);<br />

textAlign(CENTER);<br />

text(messageFromServer,width/2,140);<br />

// Fade message from server <strong>to</strong> white<br />

newMessageColor = constrain(newMessageColor + 1,0,255);<br />

// Display Instructions<br />

fill(0);<br />

text( "Type text and hit return <strong>to</strong> send <strong>to</strong> server. ",width/2,60);<br />

// Display text typed by user<br />

fill(0);<br />

text(typing,width/2,80);<br />

// If there is information available <strong>to</strong> read<br />

if (client.available() > 0) {<br />

// Read it as a String<br />

messageFromServer = client.readString();<br />

// Set brightness <strong>to</strong> 0<br />

newMessageColor = 0;<br />

}<br />

}<br />

// Simple user keyboard input<br />

void keyPressed() {<br />

// If the return key is pressed, save the String and clear it<br />

if (key == '\n') {<br />

// When the user hits enter, write the sentence out <strong>to</strong> the Server<br />

client.write(typing);<br />

typing = " " ;<br />

} else {<br />

typing = typing + key;<br />

}<br />

}<br />

Data Streams 363<br />

Exercise 19-2: Create a client and server that talk <strong>to</strong> each other. Have the client send<br />

messages typed by the user and the server respond au<strong>to</strong>nomously. For example, you could use<br />

String parsing techniques <strong>to</strong> reverse the words sent by the client. Client: “ How are you? ”<br />

Server: “ You are how? ”<br />

Broadcasting<br />

The new message fades<br />

<strong>to</strong> white by increasing<br />

the brightness.<br />

We know there is a message from<br />

the Server when there are greater<br />

than zero bytes available.<br />

When the user hits enter, the<br />

String typed is sent <strong>to</strong> the Server.<br />

Now that we understand the basics of how clients and servers work, we can examine more practical uses<br />

of networked communication. In the therapist client/server examples, we treated the data sent across the<br />

network as a String, but this may not always be the case. In this section, we will look at writing a server<br />

that broadcasts numeric data <strong>to</strong> clients.<br />

How is this useful? What if you wanted <strong>to</strong> continuously broadcast the temperature outside your house or<br />

a s<strong>to</strong>ck quote or the amount of motion seen by a camera? You could set up a single computer running a

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

Saved successfully!

Ooh no, something went wrong!