After the remotable component has been created, you can set up a server application and client using the native .NET API. For more information on choosing the right API for your access needs, see Select How to Access an Assembly.
Some reasons you might use the native .NET API instead of the MWArray
API
are:
You want to pass arguments and return values using standard .NET types, and you or your users don't work extensively with data types specific to MATLAB®.
You want to access your component from a client machine without an installed version of MATLAB.
For information on accessing your component using the MWArray
API,
see Access a Remotable .NET Assembly Using MWArray.
The server application will host the remote component you built in Create a Remotable .NET Assembly.
The client application, running in a separate process, will
access the remote component hosted by the server application. Build
the server with the Microsoft®
Visual Studio® project file MagicSquareServer\MagicSquareServer.csproj
:
Change the reference for the generated component assembly
to MagicSquareComp\for_redistribution_files_only\MagicSquareCompNative.dll
.
Select the appropriate build platform.
Select Debug or Release mode.
Build the MagicSquareServer
project.
Supply the configuration file for the MagicSquareServer
.
The C# code for the server is in the file MagicSquareServer\MagicSquareServer.cs
.
The MagicSquareServer.cs
server code is shown
here:
using System; using System.Runtime.Remoting; namespace MagicSquareServer { class MagicSquareServer { static void Main(string[] args) { RemotingConfiguration.Configure (@"..\..\..\..\MagicSquareServer.exe.config"); Console.WriteLine("Magic Square Server started..."); Console.ReadLine(); } } }
Reads the associated configuration file to determine the name of the component that it will host, the remoting protocol and message formatting to use, as well as the lease time for the remote component.
Signals that the server is active and waits for a carriage return to be entered before terminating.
The configuration file for the MagicSquareServer
is
in the file MagicSquareServer\MagicSquareServer.exe.config
.
The entire configuration file, written in XML, is shown here:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.runtime.remoting> <application> <service> <wellknown mode="SingleCall" type="MagicSquareCompNative.MagicSquareClass, MagicSquareCompNative" objectUri="MagicSquareClass.remote" /> </service> <lifetime leaseTime= "5M" renewOnCallTime="2M" leaseManagerPollTime="10S" /> <channels> <channel ref="tcp" port="1234"> <serverProviders> <formatter ref="binary" typeFilterLevel="Full" /> </serverProviders> </channel> </channels> </application> <debug loadTypes="true"/> </system.runtime.remoting> </configuration>
This code specifies:
The mode in which the remote component will be accessed—in this case, single call mode
The name of the remote component, the component assembly, and the object URI (uniform resource identifier) used to access the remote component
The lease time for the remote component
The remoting protocol (TCP/IP
)
and port number
The message formatter (binary
)
and the permissions for the communication channel (full
trust)
The server debugging option
The client application, running in a separate process, accesses
the remote component running in the server application built in Coding and Building the Hosting Server Application and Configuration File.
Build the remote client using the Microsoft
Visual Studio project
file MagicSquareClient\MagicSquareClient.csproj
.
To create the remote client using Microsoft
Visual Studio:
Change the reference for the generated component assembly
to MagicSquareComp\for_redistribution_files_only\MagicSquareCompNative.dll
.
Change the reference for the generated interface assembly
to MagicSquareComp\for_redistribution_files_only\IMagicSquareCompNative.dll
.
Select the appropriate build platform.
Select Debug or Release mode.
Build the MagicSquareClient
project.
Supply the configuration file for the MagicSquareServer
.
The C# code for the client is in the file MagicSquareClient\MagicSquareClient.cs
.
The client code is shown here:
using System; using System.Configuration; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; using System.Collections; using System.Runtime.Serialization.Formatters; using System.Runtime.Remoting.Channels.Tcp; using IMagicSquareCompNative; namespace MagicSquareClient { class MagicSquareClient { static void Main(string[] args) { try { RemotingConfiguration.Configure (@"MagicSquareClient.exe.config"); String urlServer= ConfigurationSettings.AppSettings["MagicSquareServer"]; IMagicSquareClassNative magicSquareComp= (IMagicSquareClassNative)Activator.GetObject (typeof(IMagicSquareClassNative), urlServer); // Get user specified command line arguments or set default double arraySize= (0 != args.Length) ? Double.Parse(args[0]) : 4; // Compute the magic square and print the result double[,] magicSquare= (double[,])magicSquareComp.makesquare(arraySize); Console.WriteLine("Magic square of order {0}\n", arraySize); // Display the array elements: for (int i = 0; i < (int)arraySize; i++) for (int j = 0; j < (int)arraySize; j++) Console.WriteLine ("Element({0},{1})= {2}", i, j, magicSquare[i, j]); } catch (Exception exception) { Console.WriteLine(exception.Message); } Console.ReadLine(); } } }
This code does the following:
The client reads the associated configuration file to get the name and location of the remotable component.
The client instantiates the remotable object using
the static Activator.GetObject
method
From this point, the remoting client calls methods on the remotable component exactly as it would call a local component method.
The configuration file for the magic square client is in the
file MagicSquareClient\MagicSquareClient.exe.config
.
The configuration file, written in XML, is shown here:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="MagicSquareServer" value="tcp://localhost:1234/MagicSquareClass.remote"/> </appSettings> <system.runtime.remoting> <application> <channels> <channel name="MagicSquareChannel" ref="tcp" port="0"> <clientProviders> <formatter ref="binary" /> </clientProviders> <serverProviders> <formatter ref="binary" typeFilterLevel="Full" /> </serverProviders> </channel> </channels> </application> </system.runtime.remoting> </configuration>
This code specifies:
The name of the remote component server and the remote component URI (uniform resource identifier)
The remoting protocol (TCP/IP
)
and port number
The message formatter (binary
)
and the permissions for the communication channel (full
trust)
Start the server by doing the following:
Open a DOS or UNIX® command and cd
to MagicSquareServer\bin\x86\v4.0\Debug
.
Run MagicSquareServer.exe
. You
will see the message:
Magic Square Server started...
Start the client by doing the following:
Open a DOS or UNIX command window and cd
to MagicSquareClient\bin\x86\v4.0\Debug
.
Run MagicSquareClient.exe
. After
the MATLAB Runtime initializes you should see the following output:
Magic square of order 4 Element(0,0)= 16 Element(0,1)= 2 Element(0,2)= 3 Element(0,3)= 13 Element(1,0)= 5 Element(1,1)= 11 Element(1,2)= 10 Element(1,3)= 8 Element(2,0)= 9 Element(2,1)= 7 Element(2,2)= 6 Element(2,3)= 12 Element(3,0)= 4 Element(3,1)= 14 Element(3,2)= 15 Element(3,3)= 1