
APPENDIX C 387
Test Server Program
Language: Processing
Creates a server that listens for clients and prints what
they say. It also sends the last client anything that’s typed
on the keyboard.
// include the net library:
import processing.net.*;
int port = 8080; // the port the server listens on
Server myServer; // the server object
Client thisClient; // incoming client object
void setup()
{
myServer = new Server(this, port); // Start the server
}
void draw()
{
// get the next client that sends a message:
Client speakingClient = myServer.available();
// if the message is not null, display what it sent:
if (speakingClient !=null) {
String whatClientSaid = speakingClient.readString();
// print who sent the message, and what they sent:
println(speakingClient.ip() + "\t" + whatClientSaid);
}
}
// ServerEvent message is generated when a new client
// connects to the server.
void serverEvent(Server myServer, Client someClient) {
println("We have a new client: " + someClient.ip());
thisClient = someClient;
}
void keyReleased() {
// only send if there's a client to send to:
if (thisClient != null) {
// if return is pressed, send newline and carriage feed:
if (key == '\n') {
thisClient.write("\r\n");
}
// send any other key as is:
else {
thisClient.write(key);
}
}
}
Chapter 5
Pong client
Language: Wiring/Arduino
This program enables an Arduino ...