A volte è utile raccogliere messaggi o dati in una posizione centrale da script di lunga durata o script in esecuzione su computer remoti.In allegato sono quattro file che lo dimostrano:
- Un "server" che raccoglie i messaggi da altre macchine o processi e li inserisce in una tabella di dati. Eseguilo in un'istanza separata di JMP da qualsiasi altro script o tabella.
- Uno script JSL "client" che invia messaggi al server, incorporalo negli script.
- Uno script R "client" che invia messaggi al server
- Uno script python "client" che invia messaggi al server
Ecco un esempio di come potrebbe apparire, i client sulla destra possono trovarsi su computer diversi o copie separate di JMP sullo stesso computer.

Il codice importante si presenta così:
server
//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();
Dove la funzione di callback sarebbe simile a questa (la funzione effettiva è più complessa, vedi sotto):
//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;
);
Ecco una funzione di callback leggermente più complessa che gestisce il blocco (comune):
Visualizza di più...
Ecco un esempio di un codice server completo:
Visualizza di più...
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();
I nomi predefiniti sono qui( 1 ); //Tabella per contenere i dati ricevuti dtLog = New Table( "Log", New Column( "Time", Numeric, "Continuous", Format( "m/g/yh:m:s", 23, 0 ), Input Format ("m/g/yh:m:s", 0)), Nuova colonna("Client", Carattere, "Nominale")), Nuova colonna("Messaggio", Carattere, "Nominale")); //Aspetta e ricevi i dati dalla macchina remota è connesso r = c << GetPeerName(); //Durante la ricezione di dati o in attesa di ricevere dati i = 0; While( ( (r[2] == "ok") | ((blobdata == Char To Blob( " " )) & (Left(r[2], 11) == "WOULDBLOCK:" )) ) & (i < waitcount), i++; //Get data r = c << recv( 100000 ); //Se no i dati sono ancora ricevuti, attendi If(Left(r[2], 11) == "WOULDBLOCK:", Wait( waittime ) ); //Scrivi tutto ciò che viene ricevuto nel registro Write( Blob to Char( r[3] ) ); //unisce i dati a tutto ciò che è già ricevuto blobdata = blobdata || r[3]; ); return( blobdata ); ); //Ottieni le informazioni di registro dalla macchina remota, memorizzale in una tabella di dati processlogconnection = function( {c}, //Nuova riga nella tabella di registro dtLog << Aggiungi righe ( 1 ); rowout = N righe ( dtLog ); Column ( dtLog , "Time" )[rowout] = Today(); //Ottieni il nome del computer della macchina remota r = c << GetPeerName(); Column( dtLog, "Client" )[rowout] = r[3]; //Get messaggio dalla macchina remota Column( dtLog, "Message" )[rowout] = Blob to Char( ricevuto( c ) ); //Chiudi la connessione c << Close; ); //Funzione per accettare connessioni dalla macchina remota, chiama un'altra //funzione per gestire la comunicazione con quella macchina callbackfn = function( {x}, //Chiama un'altra funzione con la connessione restituita dal messaggio Accept If( x[2] == "ok", connessioneprocesslog( x[4] ) ); ); //Crea un socket con = Socket(); //Utilizza una porta specifica su questo computer con << Bind( "10.0.0.2", "12345" ); //Di' a questa presa di essere pronta per la connessione di altre prese con << Listen(); //Verifica che la connessione sia attiva con << getsockname; //Quando qualcosa si connette a questo socket, chiama una funzione per gestire //la connessione. Accetta le connessioni per i prossimi 5 minuti. con << Accetta(callbackfn, 300); //Ora i messaggi possono essere inviati a questa connessione. //Al termine, chiudi la connessione con << close();
Cliente 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" ) );
Cliente 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")
Cliente 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;
Cliente 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()
Questo vuole essere più un'introduzione alla comunicazione socket per chiunque cerchi codice di esempio che un meccanismo di registrazione funzionale, ma accolgo con favore i tuoi commenti e suggerimenti!