This is a test server. I use it any time I have a client that needs to connect to a web server or chat server, and I want to see what the client is sending, byte for byte. You can modify it to send and receive whatever you need.
Technorati Tags: networked objects, networks
/*
server_test
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.
created 22 April 2006
by Tom Igoe
*/
// 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) {
int whatClientSaid = speakingClient.read();
// 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);
}
}
}