Skip to content

Instantly share code, notes, and snippets.

@Craigson
Created February 25, 2015 04:47
Show Gist options
  • Select an option

  • Save Craigson/47f1f17b3c8d6e5b5a17 to your computer and use it in GitHub Desktop.

Select an option

Save Craigson/47f1f17b3c8d6e5b5a17 to your computer and use it in GitHub Desktop.
Processing acting as a client, receiving data from python server
//Python acts as server
//code for processing to run as client
import processing.net.*;
Client myClient;
String dataIn;
boolean firstContact;
//code for Seiffert's spherical spiral
float r; //radius of sphere
float a; //elliptical modulus
ArrayList xVals;
ArrayList yVals;
ArrayList zVals;
int counter;
void setup() {
size(200, 200);
xVals = new ArrayList();
yVals = new ArrayList();
zVals = new ArrayList();
counter = 0; //counter for counting number of requests to python
firstContact = false; //boolean to establish first contact with server
// Connect to the local machine at port 5001.
// This example will not run if you haven't
// previously started a server on this port.
myClient = new Client(this, "127.0.0.1", 5001);
}
void draw() {
estContact();
if (firstContact == true) {
if (myClient.available() > 0 && counter < 100) {
dataIn = myClient.readString();
myClient.clear();
xVals.add(float(dataIn)); //add x-values from python to arraylist
myClient.write('A'); //request another value from server
counter++; //increment counter
myClient.clear(); //clear the buffer
} else {
myClient.stop(); //close the connection if counter is above 100
println("closing");
}
}
}
//manually request information by pressing the 'r' key
void keyPressed() {
if (key == 'r') {
myClient.write("A");
}
if (key =='p') {
println(xVals);
println(xVals.size());
}
}
void estContact() {
myClient.write('A');
firstContact = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment