長時間実行されるスクリプトやリモート コンピューターで実行されるスクリプトからメッセージやデータを中央の場所に収集すると便利な場合があります。これを示す 4 つのファイルが添付されています。
- 他のマシンまたはプロセスからメッセージを収集し、それらをデータ テーブルに入れる「サーバー」。 これを、他のスクリプトやテーブルとは別の JMP インスタンスで実行します。
- サーバーにメッセージを送信する「クライアント」JSL スクリプト。これをスクリプトに埋め込みます。
- サーバーにメッセージを送信する「クライアント」R スクリプト
- メッセージをサーバーに送信する「クライアント」python スクリプト
これがどのように表示されるかの例を次に示します。右側のクライアントは、異なるコンピューター上にある場合もあれば、同じコンピューター上に別の JMP のコピーがある場合もあります。

重要なコードは次のようになります。
サーバ
//Create a socket
con = Socket();
//Use a specific address and port on this computer
con << Bind( "10.0.0.2", "12345" );
//Tell this socket to be ready for other sockets to connect to it
con << Listen();
//Verify that the connection is active
con << getsockname;
//When something connects to this socket, call a function to handle
//the connection. Accept connections for the next 5 minutes.
con << Accept(callbackfn, 300);
//Now messages can be sent to this connection.
//When all done, close the connection
con << close();
コールバック関数は次のようになります (実際の関数はもっと複雑です。以下を参照してください)。
//Simplified function to handle communication with the client, do not use as is
callbackfn = function( {x},
If( x[2] == "ok",
//Get data
r = x[4] << recv( 1000 );
//While data was received
While( r[2] == "ok",
//Write it to the log
write( Blob to Char( r[3] ) );
//try to get more data
r = x[4] << recv( 1000 );
);
);
//close the connection to the client
x[4] << Close;
);
ブロッキングを処理するもう少し複雑なコールバック関数を次に示します (共通):
もっと見る...
完全なサーバー コードの例を次に示します。
もっと見る...
Names default to here( 1 );
//Table to hold received data
dtLog = New Table( "Log",
New Column( "Time", Numeric, "Continuous", Format( "m/d/y h:m:s", 23, 0 ),
Input Format( "m/d/y h:m:s", 0 ) ),
New Column( "Client", Character, "Nominal" ),
New Column( "Message", Character, "Nominal" )
);
//Wait for and get data from remote machine
receivedata = Function( {c, waittime = 1, waitcount = 10},
//Varaible to store data received from remote machine
blobdata = Char To Blob( "" );
//Make socket is connected
r = c << GetPeerName();
//While receiving data or waiting to receive data
i = 0;
While(
(
(r[2] == "ok") |
((blobdata == Char To Blob( "" )) & (Left(r[2], 11) == "WOULDBLOCK:" ))
) &
(i < waitcount),
i++;
//Get data
r = c << recv( 100000 );
//If no data is received yet, wait
If(Left(r[2], 11) == "WOULDBLOCK:", Wait( waittime ) );
//Write whatever is received to the log
Write( Blob to Char( r[3] ) );
//join data to whatever is already received
blobdata = blobdata || r[3];
);
return( blobdata );
);
//Get log info from remote machine, store it in a data table
processlogconnection = function( {c},
//New row in log table
dtLog << Add Rows( 1 );
rowout = N Rows( dtLog );
Column( dtLog, "Time" )[rowout] = Today();
//Get computer name of remote machine
r = c << GetPeerName();
Column( dtLog, "Client" )[rowout] = r[3];
//Get message from the remote machine
Column( dtLog, "Message" )[rowout] = Blob to Char( receivedata( c ) );
//Close the connection
c << Close;
);
//Function to accept conections from remote machine, calls another
//function to handle communication with that machine
callbackfn = function( {x},
//Call another function with the connection returned from the Accept message
If( x[2] == "ok", processlogconnection( x[4] ) );
);
//Create a socket
con = Socket();
//Use a specific port on this computer
con << Bind( "10.0.0.2", "12345" );
//Tell this socket to be ready for other sockets to connect to it
con << Listen();
//Verify that the connection is active
con << getsockname;
//When something connects to this socket, call a function to handle
//the connection. Accept connections for the next 5 minutes.
con << Accept(callbackfn, 300);
//Now messages can be sent to this connection.
//When all done, close the connection
con << close();
名前のデフォルトは here( 1 ) です。 //受信データを保持するテーブル dtLog = New Table( "Log", New Column( "Time", Numeric, "Continuous", Format( "m/d/yh:m:s", 23, 0 ), Input Format) ( "m/d/yh:m:s", 0 ) ), New Column( "Client", Character, "Nominal" ), New Column( "Message", Character, "Nominal" ) ); //リモート マシンからのデータを待機して取得 receivedata = Function( {c, waittime = 1, waitcount = 10}, //リモート マシンから受信したデータを格納する変数 blobdata = Char To Blob( "" ); //ソケットを作成接続中 r = c << GetPeerName(); //データ受信中またはデータ受信待機中 i = 0; While( ( (r[2] == "ok") | ((blobdata == Char To Blob( " " )) & (Left(r[2], 11) == "WOULDBLOCK:" )) ) & (i < waitcount), i++; //データを取得する r = c << recv( 100000 ); //いいえの場合データがまだ受信されていない場合、待機する If(Left(r[2], 11) == "WOULDBLOCK:", Wait( waittime ) ); //受信した内容をログに書き込む Write( Blob to Char( r[3] ) ); // 既に受信したものにデータを結合 blobdata = blobdata || r[3]; ); return( blobdata ); ); //リモート マシンからログ情報を取得し、データ テーブルに格納します。 , "Time" )[rowout] = Today(); //リモート マシンのコンピューター名を取得します r = c << GetPeerName(); Column( dtLog, "Client" )[rowout] = r[3]; //Getリモート マシンからのメッセージ Column( dtLog, "Message" )[rowout] = Blob to Char( receivedata( c ) ); //接続を閉じる c << Close; ); //リモート マシンからの接続を受け入れる関数は、そのマシンとの通信を処理する別の //関数を呼び出します callbackfn = function( {x}, //Accept メッセージから返された接続で別の関数を呼び出します If( x[2] == "OK", processlogconnection( x[4] ) ); ); //ソケットを作成する con = Socket(); //このコンピュータの特定のポートを使用する con << Bind( "10.0.0.2", "12345" ); //このソケットに、他のソケットが接続する準備ができていることを伝えます。 con << Listen(); //接続がアクティブであることを確認します con << getsockname; //何かがこのソケットに接続すると、 //接続を処理する関数を呼び出します。 次の 5 分間、接続を受け入れます。 con << Accept(callbackfn, 300); //これで、この接続にメッセージを送信できます。 //すべて完了したら、接続を閉じます con << close();
JSLクライアント
Names default to here( 1 );
//function to send a message to a waiting connection
SendMessage = Function( {message},
//Create a socket
con = Socket();
//Connect to a socket that is already bound to, listening on, and accepting
//connections on a port. This is the server IP address, not the client.
con << Connect( "10.0.0.2", "12345" );
//Send data
r = con << Send( Char To Blob( message ) );
///check that the data was sent
if(r[2] == "ok", result = "sent", result = "fail" );
//close the connection
con << Close;
return( result );
);
//send messages
Write( SendMessage( "Hi" ) );
R クライアント
sendmessage = function( message ) {
con <- socketConnection(host="10.0.0.2", port = 12345, blocking=FALSE,
server=FALSE, open="r+")
write_resp <- write(message, con)
close(con)
}
sendmessage("Hi from R")
Python クライアント
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("10.0.0.2", 12345))
client_socket.send("Hello from Python")
client_socket.close()
break;
Powershell クライアント
$socket = new-object System.Net.Sockets.TcpClient( "10.0.0.2", 12345)
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter $stream
$writer.WriteLine( "Hello from Powershell`n" )
$writer.Flush()
$writer.Close()
これは、機能的なロギング メカニズムよりもサンプル コードを探している人のためのソケット通信の紹介を目的としていますが、コメントや提案を歓迎します!