Processing/6.通信
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
* Processing間の通信 [#r53a4766]
Processingの間ではTCPを使った通信が可能である。その際には...
通信を行うためにはサーバのIPアドレス、ならびに、ポート番...
ポート番号は適当な番号を決めてよい。ただし、小さな番号は...
次の例は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( 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 + " "...
}
// Receive data from client
c = s.available();
if (c != null) {
input = c.readString();
input = input.substring(0, input.indexOf("\n")); // ...
data = int(split(input, ' ')); // Split values into ...
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 specif...
* address and port number to which the socket connectio...
* Once the connection is made, the client may read (or ...
* Before running this program, start the Shared Drawing...
*/
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 + " "...
}
// Receive data from server
if (c.available() > 0) {
input = c.readString();
input = input.substring(0, input.indexOf("\n")); // ...
data = int(split(input, ' ')); // Split values into ...
// Draw line using received coords
stroke(0);
line(data[0], data[1], data[2], data[3]);
}
}
終了行:
* Processing間の通信 [#r53a4766]
Processingの間ではTCPを使った通信が可能である。その際には...
通信を行うためにはサーバのIPアドレス、ならびに、ポート番...
ポート番号は適当な番号を決めてよい。ただし、小さな番号は...
次の例は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( 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 + " "...
}
// Receive data from client
c = s.available();
if (c != null) {
input = c.readString();
input = input.substring(0, input.indexOf("\n")); // ...
data = int(split(input, ' ')); // Split values into ...
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 specif...
* address and port number to which the socket connectio...
* Once the connection is made, the client may read (or ...
* Before running this program, start the Shared Drawing...
*/
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 + " "...
}
// Receive data from server
if (c.available() > 0) {
input = c.readString();
input = input.substring(0, input.indexOf("\n")); // ...
data = int(split(input, ' ')); // Split values into ...
// Draw line using received coords
stroke(0);
line(data[0], data[1], data[2], data[3]);
}
}
ページ名: