有时从长时间运行的脚本或远程计算机上运行的脚本收集消息或数据到中央位置很有用。附件是证明这一点的四个文件:
- 从其他机器或进程收集消息并将它们放入数据表中的“服务器”。 在独立于任何其他脚本或表的 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, "Continuous", Format( "m/d/yh:m:s", 23, 0 ), Input Format ( "m/d/yh:m:s", 0 ) ), 新列( "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 ); ); //从远程机器获取日志信息,存储到数据表中 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( receivedata( c ) ); //关闭连接 c << Close; ); //接受来自远程机器的连接的函数,调用另一个 //函数来处理与该机器的通信 callbackfn = function( {x}, //使用从 Accept 消息返回的连接调用另一个函数 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()
对于任何正在寻找示例代码的人来说,这更像是对套接字通信的介绍,而不是功能性日志记录机制,但我欢迎您提出意见和建议!