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.

19<br />

19.1<br />

Data Streams<br />

“ I’m mad as hell and I’m not going <strong>to</strong> take this anymore! ”<br />

—Howard Beale, Network<br />

In this chapter:<br />

– Sockets.<br />

– Servers.<br />

– Clients.<br />

– Multi-user processing.<br />

– Serial input.<br />

Synchronous vs. Asynchronous<br />

Data Streams 357<br />

In Chapter 18, we looked at how we can request the raw source of a URL using loadStrings( ), the<br />

simpleML library, or the XML library. You make the request, sit back, and await the results. You may have<br />

noticed that this process does not happen instantaneously. Sometimes the program may pause for seconds<br />

(or even minutes) while a web page or XML document loads. Th is is due <strong>to</strong> the length of time required for<br />

what <strong>Processing</strong> performs behind the scenes—an HTTP request. HTTP stands for “ Hypertext Transfer<br />

Pro<strong>to</strong>col, ” a request/response pro<strong>to</strong>col for the transfer of information on the world wide web.<br />

Let’s consider, for a moment, what we mean by “ request/response. ” Perhaps you wake up one morning<br />

and think <strong>to</strong> yourself that a vacation, say in Tuscany, is in order. You turn on your computer, launch a web<br />

browser, type www.google.com in the address bar, and enter “ romantic getaway Tuscany villa ” . You, the<br />

client , made a request, and it is the job of google, the server , <strong>to</strong> provide a response.<br />

The Client :<br />

Just <strong>to</strong> introduce myself, I'm Firefox, the web browser, and I have a request . I was<br />

wondering if you might be so kind as <strong>to</strong> send me your web page about vacation villas in<br />

Tuscany?<br />

[Dramatic Pause]<br />

The Server:<br />

Sure, no problem, here is my response . It's really just a whole lot of bytes, but if you<br />

read it as html you'll see it's a nicely formatted page about Tuscany vacation rentals.<br />

Enjoy! Oh, can you let me know that you received it ok?<br />

Got it, thanks!<br />

The Client:<br />

[The Client and the Server shake hands.]<br />

Th e above process is known as an asynchronous request and response, bi-directional communication<br />

between a server and a client where the server responds at its leisure. Th e connection is established<br />

temporarily in order <strong>to</strong> transfer the web page after which it is promptly closed.<br />

In terms of sending information along the world wide web, this methodology is perfectly adequate.<br />

Nevertheless, imagine if you were <strong>to</strong> use asynchronous communication in a multiplayer game. It would

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

Saved successfully!

Ooh no, something went wrong!