Communicate Between a TCP/IP Client and Server in MATLAB
This example shows how to use the tcpserver
and tcpclient
functions to create a TCP/IP client and TCP/IP server in MATLAB®
and then send data between them over the TCP/IP protcol. 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. Copy the Client Session section to another MATLAB script in the second MATLAB session. Run the Server Session script first and then run the Client Session script, 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 the Address
and Port
values for creating the tcpclient
object in the Client Session.
Server Session
In this session, create a tcpserver
object that listens for client connection requests. It sends data after a client connects to it. It also uses the callback functionality enabled by the configureCallback
method to read data sent by the client.
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. 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,"ConnectionChangedFcn",@connectionFcn)
server = TCPServer with properties: ServerAddress: "172.28.200.248" ServerPort: 5000 Connected: 0 ClientAddress: "" ClientPort: [] NumBytesAvailable: 0 Show all properties, functions
Read Binary Data Using Byte Callback Mode
Create a callback function called readDataFcn
to read data each time the specified bytes of data are available. Store the read data in the UserData
property of tcpserver
object. You can find the readDataFcn
function at the end of this example.
Set the callback function to trigger each time 7688 bytes of data are received.
configureCallback(server,"byte",7688,@readDataFcn);
Client Session
In this session, create a tcpclient
object to connect to the server. The client reads data sent from the server. It then sends the data it read back to the server.
Create Client
Create a tcpclient
instance 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 use them as the Address
and Port
values for creating the tcpclient
object.
client = tcpclient(server.ServerAddress,server.ServerPort,"Timeout",5)
client = tcpclient with properties: Address: '172.28.200.248' Port: 5000 NumBytesAvailable: 0 Show all properties, functions
pause(1);
Read Data and Display
Read data sent by the server. Reshape the data array and plot it.
rawData = read(client,961,"double");
reshapedData = reshape(rawData,31,31);
surf(reshapedData);
Write Data
Write data to the server.
write(client,rawData,"double");
Clear the Client
Clear the tcpclient
instance.
clear client
Callback Functions
Connection Callback Function to Write Binary Data
This function calls write
to write data to the connected TCP/IP client.
function connectionFcn(src, ~) if src.Connected disp("Client connection accepted by server.") data = membrane(1); write(src,data(:),"double"); end end
Data Available Callback Function to Read Binary Data
This function calls read
to read BytesAvailableFcnCount
number of bytes of data.
function readDataFcn(src, ~) disp("Data was received from the client.") src.UserData = read(src,src.BytesAvailableFcnCount/8,"double"); reshapedServerData = reshape(src.UserData,31,31); surf(reshapedServerData); end