FUJILOG

見た、聴いた、触れたこと。 動かしたもの、書いたもの。 ウェブとリアルの備忘録です。

perl/processing で client/server

下記構成で試してみました。
TCP経由でのデータ連携のコードです。

クライアント処理: Perl
サーバ処理: Processing
port番号: 12345


【クライアント】
#!/usr/bin/perl
use strict;
use IO::Socket;

my $sock = new IO::Socket::INET(PeerAddr=>'localhost',
 PeerPort=> 12345,
 Proto=>'tcp');

die "IO::Socket : $!" unless $sock;

$sock -> print("hello world\n"); # 送信用の文字列を用意
print $sock -> getline; # サーバに文字列を送信

close($sock);



【サーバ】
/***
TCP:12345 portで受信したStringをprintlnする
Compiler: Proce55ing ver1.0.1 later
Date: 2009/10/08
***/
import processing.net.*;

int port = 12345;
Server server;
String str;

void setup(){
 size(200,200);
 server = new Server(this, port);
}

void draw(){
 Client client = server.available();
 
 /* Clientがある場合 */
 if(client != null){
  if*1 != null){ // 文字列を送ってきているなら受信
  print(str);
  client.write("echo: "+str); // Clientに送り返す
   }
 }
}

/* Clientが接続した時 */
void serverEvent(Server s, Client c){
println("Client connect - IP:"+c.ip()); // IPアドレス表示
}



サーバプログラム実行し、サーバ起動中にクライアントプログラムを実行すれば、サーバがリスポンスを返します。

*1:str=client.readString(