how to use the network library correct?
in
Core Library Questions
•
2 years ago
Hi!
I tried this two codes form the daniel shiffman examples with the server and client example using the network library. Seems to work fine if i use the rigth IP adress from one to another computer. But how can i handle it to sending some datas over wide distances, like china... ? Didnt work, even i used the right IP adress... what could be the problem, it should be possible, right?
I tried this two codes form the daniel shiffman examples with the server and client example using the network library. Seems to work fine if i use the rigth IP adress from one to another computer. But how can i handle it to sending some datas over wide distances, like china... ? Didnt work, even i used the right IP adress... what could be the problem, it should be possible, right?
- // SERVER
- //
- import processing.net.*;
- // Declare a server
- Server server;
- PFont f;
- int data = 0;
- void setup() {
- size(200,200);
- // Create the Server on port 5204
- server = new Server(this, 5204);
- f = createFont("Arial",20,true);
- }
- void draw() {
- background(255);
- // Display data
- textFont(f);
- textAlign(CENTER);
- fill(0);
- text(data,width/2,height/2);
- // Broadcast data (the number is continuously sent to all clients because write() is called every cycle through draw())
- server.write(data);
- // Arbitrarily changing the value of data randomly
- data = (data + 1) % 256;
- }
- // The serverEvent function is called whenever a new client connects.
- void serverEvent(Server server, Client client) {
- println(" A new client has connected: "+ client.ip());
- }
- // CLIENT
- //
- // Import the net libraries
- import processing.net.*;
- // Declare a client
- Client client;
- // The data we will read from the server
- int data;
- void setup() {
- size(200,200);
- smooth();
- // Create the Client
- client = new Client(this,"127.0.0.1", 5204); //local host
- }
- void draw() {
- // Read data
- if (client.available() > 0) {
- data = client.read();
- }
- background(255);
- stroke(0);
- fill(175);
- translate(width/2,height/2);
- // The incoming data is used to rotate a square.
- float theta = (data/255.0) * TWO_PI;
- rotate(theta);
- rectMode(CENTER);
- rect(0,0,64,64);
- }
2