通信するプログラムを作るときには net ライブラリを使用します。メニューから Sketch > Import Library > net を選択すると
import processing.net.*;
という1行がプログラムに挿入されますのでこれで準備完了です。
実行するときはまず Server を実行してから、その後 Client を実行してください。Client 側の serverAddress を書き換えれば他のコンピュータに接続することができます。
import processing.net.*; Server server; void setup() { server = new Server(this, 5555); println("start server at address: " + server.ip()); } void draw() { Client c = server.available(); if (c != null) { String s = c.readStringUntil('\n').trim(); println("received:" + s); String[] data = s.split(","); int x = parseInt(data[0]); int y = parseInt(data[1]); server.write(x + "," + y + '\n'); } }
import processing.net.*; String serverAddress = "localhost"; Client c; ArrayList<PVector> points; void setup() { points = new ArrayList<PVector>(); c = new Client(this, serverAddress, 5555); } void draw() { background(0); fill(255); for(int i = 0; i < points.size(); i++){ PVector p = points.get(i); ellipse(p.x, p.y, 5, 5); } } void mouseClicked() { c.write(mouseX + "," + mouseY + '\n'); } void clientEvent(Client c) { String s = c.readStringUntil('\n'); if (s != null) { print("received from server: " + s); String[] data = s.trim().split(","); int x = parseInt(data[0]); int y = parseInt(data[1]); points.add(new PVector(x, y)); } }