Using MathLink to Communicate between Mathematica Sessions

LinkCreate["name"]create a link for another program to connect to
LinkConnect["name"]connect to a link created by another program
LinkClose[link]close a MathLink connection
LinkWrite[link,expr]write an expression to a MathLink connection
LinkRead[link]read an expression from a MathLink connection
LinkRead[link,Hold]read an expression and immediately wrap it with Hold
LinkReadyQ[link]find out whether there is data ready to be read from a link
LinkReadyQ[link,t]wait for up to t seconds to see if an expression becomes ready to read
LinkReadyQ[{link1,link2,...}]find out whether there is data ready to be read from one of the links
LinkReadyQ[{link1,link2,...},t]wait for up to t seconds to see if an expression becomes ready to read

MathLink connections between Mathematica sessions.

Session A

This starts up a link on port number 8000.
In[1]:=
Click for copyable input
Out[1]=

Session B

This connects to the link on port 8000.
In[2]:=
Click for copyable input
Out[2]=

Session A

This evaluates and writes it to the link.
In[3]:=
Click for copyable input

Session B

This reads from the link, getting the that was sent.
In[4]:=
Click for copyable input
Out[4]=
This writes data back on the link.
In[5]:=
Click for copyable input

Session A

And this reads the data written in session B.
In[6]:=
Click for copyable input
Out[6]=

One use of MathLink connections between Mathematica sessions is simply as a way to transfer data without using intermediate files.

Another use is as a way to dispatch different parts of a computation to different sessions.

Session A

This writes the expression without evaluating it.
In[7]:=
Click for copyable input

Session B

This reads the expression from the link, immediately wrapping it in Hold.
In[8]:=
Click for copyable input
Out[8]=
This evaluates the expression.
In[9]:=
Click for copyable input
Out[9]=

When you call LinkWrite, it writes an expression to the MathLink connection and immediately returns. But when you call LinkRead, it will not return until it has read a complete expression from the MathLink connection.

You can tell whether anything is ready to be read by calling LinkReadyQ[link]. If LinkReadyQ returns True, then you can safely call LinkRead and expect immediately to start reading an expression. But if LinkReadyQ returns False, then LinkRead would block until an expression for it to read had been written by a LinkWrite in your other Mathematica session.

Session A

There is nothing waiting to be read on the link, so if LinkRead were to be called, it would block.
In[10]:=
Click for copyable input
Out[10]=

Session B

This writes an expression to the link.
In[11]:=
Click for copyable input

Session A

Now there is an expression waiting to be read on the link.
In[12]:=
Click for copyable input
Out[12]=
LinkRead can thus be called without fear of blocking.
In[13]:=
Click for copyable input
Out[13]=

LinkReadyQ can take a list of link objects, evaluating each link in parallel to determine if there is data to read. As in the case of a single link, a second argument specifies a time out period, causing LinkReadyQ to wait until one of the links is ready to use.

LinkCreate[LinkProtocol->"TCPIP"]pick any unused port on your computer
LinkCreate["number",LinkProtocol->"TCPIP"]
use a specific port
LinkConnect["number",LinkProtocol->"TCPIP"]
connect to a port on the same computer
LinkConnect["number@host",LinkProtocol->"TCPIP"]
connect to a port on another computer

Ways to set up MathLink links over TCP/IP.

MathLink can use whatever mechanism for interprogram communication your computer system supports. In setting up connections between concurrent Mathematica sessions, a common mechanism is internet TCP ports.

Most computer systems have a few thousand possible numbered ports, some of which are typically allocated to standard system services.

You can use any of the unallocated ports for MathLink connections.

Session on frog.wolfram.com

This finds an unallocated port on .
In[14]:=
Click for copyable input
Out[14]=

Session on toad.wolfram.com

This connects to the port on .
In[15]:=
Click for copyable input
Out[15]=
This sends the current machine name over the link.
In[16]:=
Click for copyable input

Session on frog.wolfram.com

This reads the expression written on .
In[17]:=
Click for copyable input
Out[17]=

By using internet ports for MathLink connections, you can easily transfer data between Mathematica sessions on different machines. All that is needed is that an internet connection exists between the machines.

Note that because MathLink is completely system independent, the computers at each end of a MathLink connection do not have to be of the same type. MathLink nevertheless notices when they are, and optimizes data transmission in this case.

New to Mathematica? Find your learning path »
Have a question? Ask support »