IAT267 Multiuse Server Example.docx

4 Pages
Unlock Document

Simon Fraser University
Interactive Arts & Tech
IAT 267
Helmine Serban

Multiuse Server Server Code // Import the net libraries import processing.net.*; // Declare a server Server server; PFont f; String incomingMessage = ""; void setup() { size(400,200); // Create the Server on port 5204 server = new Server(this, 5204); f = createFont("Arial",20,true); } void draw() { background(255); // Display rectangle with new message color fill(0); textFont(f); textAlign(CENTER); text(incomingMessage,width/2,height/2); // If a client is available, we will find out // If there is no client, it will be"null" Client client = server.available(); // We should only proceed if the client is not null if (client != null) { // Receive the message incomingMessage = client.readStringUntil('*'); // Print to Processing message window println( "Client says:" + incomingMessage); // Write message back out (note this goes to ALL clients) server.write(incomingMessage); } } // The serverEvent function is called whenever a new client connects. void serverEvent(Server server, Client client) { incomingMessage = "A new client has connected:" + client.ip(); println(incomingMessage); } Client1 Code // Import the net libraries import processing.net.*; // Declare a client Client client; void setup() { size(200,200); // Create the Client client = new Client(this, "" , 5204); background(255); smooth(); } void draw() { // If there is information available to read from the Server if (client.available() > 0) { // Read message as a String, all messages end with an asterisk String in = client.readStringUntil('*'); // Print message received println( "Receiving:" + in); // The client reads messages from the Server and parses them with splitTokens() according to our protocol. int
More Less

Related notes for IAT 267

Log In


Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.