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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

362 <strong>Learning</strong> <strong>Processing</strong><br />

import processing.net.*;<br />

Client client;<br />

Th e client construc<strong>to</strong>r requires three arguments— “ this ” , referring again <strong>to</strong> this PApplet , the IP address we<br />

want <strong>to</strong> connect <strong>to</strong> (as a String), and the port number (as an integer).<br />

client = new Client(this, " 127.0.0.1 " , 5204);<br />

If the server is running on a diff erent computer than the client, you will need <strong>to</strong> know the IP address of<br />

that server computer. In addition, if there is no server running at the specifi ed IP and port, the <strong>Processing</strong><br />

sketch will give the error message: “ java.net.ConnectException: Connection refused ” meaning either the<br />

server rejected the client or that there is no server.<br />

Sending <strong>to</strong> the server is easy using the write( ) function.<br />

client.write( " Hello! " );<br />

Reading messages from the server is done with the read( ) function. Th e read( ) method, however, reads<br />

from the server one byte at a time. To read the entire message as a String, readString( ) is used.<br />

Before we can even contemplate reading from the server, we must be sure there is something <strong>to</strong> read. Th is<br />

check happens with available( ). available( ) returns the number of bytes that are waiting <strong>to</strong> be read. We can<br />

determine if there is anything waiting <strong>to</strong> be read by asking if the number of bytes is greater than zero.<br />

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

String message = client.readString();<br />

}<br />

Using the code from Example 18-1 , (keyboard input), we can create a <strong>Processing</strong> client that connects and<br />

communicates with our server, sending messages entered by the user. See Example 19-2.<br />

Example 19-2: Simple therapy client<br />

// Import the net libraries<br />

import processing.net.*;<br />

// Declare a client<br />

Client client;<br />

// Used <strong>to</strong> indicate a new message<br />

float newMessageColor = 0;<br />

// A String <strong>to</strong> hold whatever the server says<br />

String messageFromServer = " " ;<br />

// A String <strong>to</strong> hold what the user types<br />

String typing = " " ;<br />

PFont f;<br />

void setup() {<br />

size(400,200);<br />

// Create the Client<br />

client = new Client(this, " 127.0.0.1 " , 5204);<br />

f = createFont( " Arial " ,16,true);<br />

}<br />

Type text and hit return <strong>to</strong> send <strong>to</strong> server.<br />

fi g. 19.3<br />

How does processing make you feel?<br />

Connect <strong>to</strong> server at 127.0.0.1<br />

(localhost), port 5204

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

Saved successfully!

Ooh no, something went wrong!