/* server_stop by Tom Igoe This example creates a server that listens for clients and prints what they say. Clicking the mouse stops or starts the server To test this client, open a terminal window (OSX) or a command window (Windows) and type "telnet localhost 10002" or "telnet 127.0.0.1 10002" or "telnet 10002" And you will be connected. Then type some text, hit the return key, and your text will be sent to the server. Created 3 June 2005 Updated 13 July 2005 Bug note: stopping the server produces an unrecoverable error: "java.net.SocketException: Socket closed at java.net.PlainSocketImpl.socketAccept(Native Method)" */ import processing.net.*; PFont font; int port = 10002; // the port the server listens on boolean myServerRunning; // status of the server int textLine = 60; // Y position of last text line Server myServer; // the server void setup() { size(400, 400); background(0); // Replace this with your own font: font = loadFont("GaramondPremrPro-18.vlw"); textFont(font, 18); myServerRunning = false; text("Server Running:" + "\t" + myServerRunning, 15, 25); } void mousePressed() { // If the mouse clicked the myServer changes status println("click"); if (myServerRunning) { // N.B. This produces an error which kills the applet. myServerRunning = false; myServer.stop(); myServer = null; } else { myServer = new Server(this, port); // Starts a server on port 10002 myServerRunning = true; } background(0); text("Server Status:" + "\t" + myServerRunning, 15, 25); } void draw() { if (myServerRunning) { // get the next available client: Client thisClient = myServer.available(); // if the client is not null, and says something, display what it said: if (thisClient !=null) { String whatClientSaid = thisClient.readString(); if (whatClientSaid != null) { text(thisClient.ip() + "\t" + whatClientSaid, 15, textLine); textLine = textLine + 20; } } } }