This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)

Documentation / Mathematica / Add-ons & Links / MathLink / MathLink Introduction /

2.13.8 Special Topic: Communication with Mathematica Front Ends

The Mathematica kernel uses MathLink to communicate with Mathematica front ends. If you start a Mathematica kernel from within a front end, therefore, the kernel will be controlled through a MathLink connection to this front end.

The link to the front end for a particular kernel.

The global variable $ParentLink specifies the MathLink connection that a particular kernel will use for input and output.

It is sometimes useful to reset $ParentLink in the middle of a Mathematica session, thereby effectively changing the front end to which the kernel is connected.

Session A

This creates a link on port 8000.

In[1]:= link = LinkCreate["8000"]

Out[1]=LinkObject[8000@frog.wolfram.com, 4, 4]

Session B

This connects to the link opened in session A.

In[2]:= LinkConnect["8000"]

Out[1]=LinkObject[8000@frog.wolfram.com, 4, 4]

This tells session B that it should use session A as a front end.

In[3]:= $ParentLink = %

Session A

Session A now acts as a front end to session B and gets all output from it.

In[4]:= Table[LinkRead[link], {4}]

Out[2]={ResumePacket[LinkObject[ParentLink, 1, 1]],
OutputNamePacket[Out[2]= ], ReturnTextPacket[
LinkObject[8000@frog.wolfram.com, 4, 4]],
InputNamePacket[In[3]:= ]}

This releases session B again.

In[5]:= LinkWrite[link, EnterTextPacket["$ParentLink=."]]

Much like the Mathematica kernel, the standard notebook front end for Mathematica is set up to handle a certain set of MathLink packets.

Usually it is best to use functions like NotebookWrite and FrontEndExecute if you want to control the Mathematica front end from the kernel. But in some cases you may find it convenient to send packets directly to the front end using LinkWrite.