|On this page…|
Support for Server Sockets is available, using the NetworkRole property on the TCP/IP interface. This support is for a single remote connection. You can use this connection to communicate between a client and MATLAB®, or between two instances of MATLAB.
For example, you might collect data such as a waveform into one instance of MATLAB, and then want to transfer it to another instance of MATLAB.
Note: The use of the server socket on either the client or server side should be done in accordance with the license agreement as it relates to your particular license option and activation type. If you have questions, you should consult with the administrator for your license or your legal department.
This is intended for use behind a firewall on a private network.
Note that while a server socket is waiting for a connection after calling fopen, the MATLAB processing thread is blocked. To stop fopen or to stop listening for connections, and restore the use of MATLAB, type Ctrl+C at the MATLAB command line.
To use this feature it is necessary to set the NetworkRole property in the tcpip interface. It uses two values, client and server, to establish a connection as the client or the server. The server sockets feature supports binary and ASCII transfers.
The following example shows how to connect two MATLAB sessions on the same computer, showing the example code for each session. To use two different computers, replace 'localhost' with the IP address of the server in the code for Session 2. Using '0.0.0.0' as the IP address means that the server will accept the first machine that tries to connect. To restrict the connections that will be accepted, replace '0.0.0.0' with the address of the client in the code for Session 1.
Session 1: MATLAB Server
Accept a connection from any machine on port 30000.
t=tcpip('0.0.0.0', 30000, 'NetworkRole', 'server');
Open a connection. This will not return until a connection is received.
Read the waveform and confirm it visually by plotting it.
data=fread(t, t.BytesAvailable); plot(data);
Session 2: MATLAB Client
This code is running on a second copy of MATLAB.
Create a waveform and visualize it.
Create a client interface and open it.
t=tcpip('localhost', 30000, 'NetworkRole', 'client'); fopen(t)
Write the waveform to the server session.