learning processing

Page 380

Data Streams

361

Once the server is running, we can create a client that connects to the server. Ultimately, we will look at an example where we write both the server and client in Processing. However, just to demonstrate that the server is in fact working, we can connect to it using any telnet client application. Telnet is a standard protocol for remote connections and all machines generally come with built-in telnet abilities. On a Mac, launch terminal, on Windows, go to the command prompt. I also recommend using PuTTY, a free telnet client: http://www.chiark.greenend.org.uk/~sgtatham/putty/. Since we are connecting to the server from the same machine that the server is running on, the address we telnet to is localhost, meaning the local computer, port 5,204. We could also use the address 127.0.0.1. This is a special address reserved for programs on a computer to speak to each other locally (i.e., on the same machine) and is the equivalent of localhost. If we were connecting from a different computer, we would have to know the network IP address of the machine running the server.

fig. 19.2

Telnet clients traditionally send messages to the server when the user types enter. The carriage return and line feed are included in the message and therefore when the server sends back the reply, you will notice that “How does processing” and “make you feel” appear on separate lines.

Exercise 19-1: Using String manipulation techniques from Chapter 15, fix Example 19-1 so that if the client sends newline characters, the server removes them before replying back to the client. You will want to alter the “incomingMessage” variable. incomingMessage = client.readString(); incomingMessage = incomingMessage. ______ (______, ______);

19.3 Creating a Client Once we have written a server and tested it with telnet, we can then develop our own client in Processing. We start off the same way we did with a server, importing the net library and declaring an instance of a Client object.


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.