Main Content

Communicate Between TCP/IP Client Receive Block and TCP/IP Server in MATLAB

This example shows how to use the tcpserver function and TCP/IP Receive block to create a TCP/IP server and TCP/IP client in MATLAB® and then send data between them over the TCP/IP protocol. You can run this example three different ways:

  • Within a single MATLAB session.

  • Between two MATLAB sessions on the same computer.

  • Between two MATLAB sessions on different computers that are part of the same subnet.

To run this example in a single MATLAB session, you do not have to make any modifications. Create both the server and client in the same MATLAB session.

To run this example in two different MATLAB sessions either on the same computer or on two different computers, you have to run the specified sections in each MATLAB session. Copy the Server Session and Callback Functions sections to one MATLAB script in the first MATLAB session. Open the Simulink® model from the Client Session in the second MATLAB session. Run the Server Session script first and then run the Client Session model, since you must create the server before a client can attempt a connection to the server.

When using two MATLAB sessions, copy the values of server.ServerAddress and server.ServerPort from Server Session and use them as values for the Remote address and Port parameters in the TCP/IP Client Receive block in the Client Session.

Server Session

In this session, you create a tcpserver object that listens for client connection requests. It sends data after a client connects to it.

Find Host Name and Address

Find the host name and address of the machine where the server is created. The client uses this address to connect to the server.

[~,hostname] = system("hostname");
hostname = string(strtrim(hostname));
address = resolvehost(hostname,"address");

Create Server

Create the tcpserver object using the address of the machine and port 5000. If you are running the TCP/IP server and client from the same MATLAB session, you can specify the address as localhost.

Create a callback function called connectionFcn to write data when a TCP/IP client connects to the server. Set the ConnectionChangedFcn property to the callback function connectionFcn. You can find the connectionFcn function at the end of this example.

server = tcpserver(address,5000,ByteOrder="big-endian",ConnectionChangedFcn=@connectionFcn)
server = 
  TCPServer with properties:

        ServerAddress: ""
           ServerPort: 5000
            Connected: 0
        ClientAddress: ""
           ClientPort: []
    NumBytesAvailable: 0

  Show all properties, functions

Client Session

In this session, connect to the server by running the model with the TCP/IP Client Receive block. The client block reads data sent from the server.

Create Client

Open the model and set the timeout to five seconds.

When using two MATLAB sessions, copy the values of server.ServerAddress and server.ServerPort from Server Session and specify them as the Remote address and Port values for the TCP/IP Client Receive block.

Use the following command to open the model.


You can specify the Remote address and Port values from the block parameters in the model. You can also specify the values of these parameters using the following command.

set_param("demoinstrsl_tcpipServerCommunication/TCP//IP Receive",Host=server.ServerAddress,Port=num2str(server.ServerPort),Timeout="5")

Run the TCP/IP client model using the following command.


Save and Close Model

The model displays the data sent to the TCP/IP Client Receive block from the connected TCP/IP server. In this example, the server is configured to write a sine wave.

When you are finished, use the following commands to save and close the model.


Callback Functions

This connection callback function calls write to write binary data to the connected TCP/IP client.

function connectionFcn(src, ~)
if src.Connected
    freq = 1;   % frequency (Hz)
    time = (0:1/(freq*100):1);
    amp = 1;    % amplitude (V)
    phi = 0;    % phase
    data = amp*sin(2*pi*freq*time+phi);

See Also

| |

Related Topics