#author("2020-10-21T13:22:52+09:00","default:qua","qua")
#author("2020-10-21T13:27:07+09:00","default:qua","qua")
* Processing間の通信 [#r53a4766]
Processingの間ではTCPを使った通信が可能である。その際には1台のサーバが1台以上のクライアントと通信を行う。
通信を行うためにはサーバのIPアドレス、ならびに、ポート番号が必要である。IPアドレスは各PCに割り振られた番号である。Windowsでは、コマンドプロンプトを開いて(左下の検索でcmdを入力)、ipconfigと入力して、IPv4 アドレス.... に続いて得られる4つの数字(例えば172.31.23.5)である。
ポート番号は適当な番号を決めてよい。ただし、小さな番号は他の通信で使われているので、50000から60000くらいの番号にしておく

次の例はIPアドレスが172.31.23.123のサーバがポート56789を使ってクライアントと通信を行う。

** サーバ側のプログラム [#i07f70de]
 import processing.net.*;
 Server s;
 Client c;
 String input;
 void setup(){
   s = new Server( this, 56789 );
 }
 void draw(){
   c = s.available();
   if (c != null) {
     if( c.available()>0) {
       input = c.readString();
       println( input );
       s.write( "Received: " + input );
     }
   }
 }

** クライアント側のプログラム [#w0050bcd]
 import processing.net.*;
 Client c;
 String input;
 void setup(){
   c = new Client( this, "172.31.23.123", 56789 );
 }
 void draw(){
   if( keyTyped ){
   c.write( key );
   if( keyPressed ){
     c.write( key );
   }
   if (c.available() > 0) {
     input = c.readString();
     println( input );
   }
 }



- サーバ側のプログラム
 import processing.net.*;
 Server s;
 Client c, c0;
 String input;
 int data[];
 
 void setup(){
   size(450, 255);
   background(204);
   stroke(0);
   frameRate(5); // Slow it down a little
 
   // Connect to the server's IP address and port
   c0 = new Client( this, "172.31.23.5", 54321 );
   c0.write( "Spbl2020" );
   while( c0.available() == 0 ){
     delay( 100 );
   }
   String x = c0.readString();
   c0.stop();
   s = new Server( this, 56789 );
 }
 
 void draw(){
   if (mousePressed == true) {
     // Draw our line
     stroke(255);
     line(pmouseX, pmouseY, mouseX, mouseY);
     // Send mouse coords to other person
     s.write(pmouseX + " " + pmouseY + " " + mouseX + " " + mouseY + "\n");
   }
   // Receive data from client
   c = s.available();
   if (c != null) {
     input = c.readString();
     input = input.substring(0, input.indexOf("\n")); // Only up to the newline
     data = int(split(input, ' ')); // Split values into an array
     print( data[0]+" "+data[1] );
     // Draw line using received coords
     stroke(0);
     line(data[0], data[1], data[2], data[3]);
   }
 }

- クライアント側のプログラム
 /**
  * Shared Drawing Canvas (Client) 
  * by Alexander R. Galloway. 
  * 
  * The Processing Client class is instantiated by specifying a remote 
  * address and port number to which the socket connection should be made. 
  * Once the connection is made, the client may read (or write) data to the server.
  * Before running this program, start the Shared Drawing Canvas (Server) program.
  */
 
 
 import processing.net.*;
 
 Client c=null, c0;
 String input;
 int data[];
 
 void setup() 
 {
   size(450, 255);
   background(204);
   stroke(0);
   frameRate(5); // Slow it down a little
 
   // Connect to the server's IP address and port
   c0 = new Client( this, "172.31.23.5", 54321 );
   c0.write( "Rpbl2020" );
   while( c0.available() == 0 ){
     delay( 100 );
   }
   String x = c0.readString();
   c0.stop();
   c = new Client( this, x, 56789 );
 }
 
 void draw() 
 {
   if (mousePressed == true) {
     // Draw our line
     stroke(255);
     line(pmouseX, pmouseY, mouseX, mouseY);
     // Send mouse coords to other person
     c.write(pmouseX + " " + pmouseY + " " + mouseX + " " + mouseY + "\n");
   }
   // Receive data from server
   if (c.available() > 0) {
     input = c.readString();
     input = input.substring(0, input.indexOf("\n")); // Only up to the newline
     data = int(split(input, ' ')); // Split values into an array
     // Draw line using received coords
     stroke(0);
     line(data[0], data[1], data[2], data[3]);
   }
 }

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS