Implementation of the simple protocol to transfer arbitrary data over TCP between two kernels.
- No handshaking
- First 4 bytes - the length of the message (unsigned int)
- The rest is payload
- No additional separators
- Fully asynchronous
- It works effectively with byte arrays
- TCP packet size doesn't matter
It is performed simply via Import
Import["https://raw.githubusercontent.com/JerryI/tcp-mathematica/main/JTP/JTP.wl"];
The examples are stored in test.nb
.
It is always listening for data from multiple clients. Firstly, you need to create server by
server = JTPServer[] // JTPServerStart
The host address and the port can be specified by "host"
and "port"
options. By the default, it starts to listerning. It evaluates every received message (not TCP packet) and replies.
One can access to the logs using
server["log"]["Elements"] // TableForm // Dynamic
You can use it in the same manner
client = JTPClient["host"->"", "port"->] // JTPClientStart
There are a few different modes how to work with server:
The simples model is like Evaluate
in the notebook interface
JTPClientEvaluate[client, expression]
The second argument has Hold
attribute on èxpression
. After evaluating on the server it will return the result.
The same as the previous, but doen't stop the program and accepts callback[uuid, result]
function to handle the answer from the server.
JTPClientEvaluateAsync[client, expression, Promise -> callback]
If you don't care about the result - use
JTPClientSend[cli, socket]
To start contineous listerning mode on the client use
JTPClientStartListening[cli, Promise -> callback]
If the callback
option is not specified, then only the evaluation of the incomming answers will be performed. Note that in this mode the methods (1), (2) are disabled.
Other notes on everything...
The evaluation of the incomming messages on the client and server either performes inside Block[]
with defined variable socket = uuid
, where uuid
is id of the socket, which sent the message. It can be obtained by
JTPClientEvaluate[cli, socket] == "0e5eac ... "
And to reply to yourself using low-level functions on the server's side
JTPClientStartListening[cli, Promise -> Function[{x, y}, CreateWindow[DialogNotebook[y]]]]
JTPClientSend[cli, reply[socket, "ahaha"]; "ohoho"];
This part is still in development...
For this reason you need to obtain uuid of the socket first
JTPSend[uuid, expr]
This part is still in development...