BookmarkSubscribeRSS Feed
ih

Community Trekker

Joined:

Sep 30, 2016

Socket communication to get and log messages from JMP, R, and python scripts

It is sometimes useful to collect messages or data in a central location from long running scripts, or scripts running on remote computers.  Attached are four files that demonstrate this:

 

  1. A 'server' which collects messages from other machines or processes and puts them in a data table. Run this in a separate instance of JMP from any other scripts or tables.
  2. A 'client' JSL script which sends messages to the server, embed this in scripts.
  3. A 'client' R script which sends messages to the server
  4. A 'client' python script which sends messages to the server

 

The important code looks like this:

 

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();

 

Where the callback function would look something like this (the actual function is more complex):

//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;
);

 

JSL Client

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 Client

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 Client

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;

 

This is intended to be more of an introduction to socket communication for anyone looking for example code than a functional logging mechanism but I welcome your comments and suggestions!

Article Tags
Contributors