有時,從長時間運行的腳本或遠端電腦上執行的腳本收集集中位置的訊息或資料很有用。隨附的四個文件證明了這一點:
- 一個“伺服器”,從其他機器或進程收集訊息並將它們放入資料表中。 在與任何其他腳本或表格不同的 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();
名稱預設為此處( 1 ); //儲存接收到的資料的表dtLog = New Table( "Log", New Column( "Time", Numeric, "Continious", Format( "m/d/yh:m:s", 23, 0 ),輸入格式( "m/d/yh:m:s", 0 ) ), 新列( "客戶", 字元, "名義" ), 新列( "訊息", 字元, "名義" ) ); //等待並從遠端機器取得資料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 ); ); //從遠端機器取得日誌訊息,將其儲存在資料表中processlogconnection = function( {c}, //日誌表中的新行dtLog << Add Rows( 1 ); rowout = N Rows( dtLog ); Column ( dtLog , "Time" )[rowout] = Today(); //取得遠端機器的電腦名稱r = c << GetPeerName(); Column( dtLog, "Client" )[rowout] = r[3]; / /取得來自遠端電腦的訊息 Column( dtLog, "Message" )[rowout] = Blob to Char( receiveata( c ) ); //關閉連線 c << Close; ); //接受來自遠端機器的連接的函數,呼叫另一個//函數來處理與該機器的通訊callbackfn = function( {x}, //使用從接受訊息傳回的連線呼叫另一個函數If( x[2 ] == “確定”,processlogconnection(x[4]));); //建立一個套接字 con = Socket(); //使用這台電腦上的特定連接埠 con << Bind( "10.0.0.2", "12345" ); //告訴這個套接字準備好讓其他套接字連接到它 con << Listen(); //驗證連線是否處於活動狀態 con << getsockname; //當有東西連接到此套接字時,呼叫一個函數來處理 //連接。 在接下來的 5 分鐘內接受連線。 con << 接受(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()
這更多的是為任何尋找示例代碼的人介紹套接字通信,而不是功能性日誌記錄機制,但我歡迎您的評論和建議!