Main Content

Communicate Binary and ASCII Data to an Echo Server Using TCP/IP

This example shows how to set up an echo server and communicate with it using TCP/IP by creating a tcpclient object. Binary data and terminated string data are sent to the server and the server echoes the same data back to the client.

Set Up TCP/IP Echo Server and Client

Create a TCP/IP echo server on port 4500.

echotcpip("on",4500);

Create a tcpclient object and connect to the server. Specify the remote host as "localhost" to connect to the echo server. Specify the same remote port number you used for the echo server.

t = tcpclient("localhost",4500)
t = 
  tcpclient with properties:

              Address: 'localhost'
                 Port: 4500
    NumBytesAvailable: 0

  Show all properties, functions

Write and 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 tcpclient object. See the readDataFcn function at the end of this example.

Set the callback function to trigger each time 10 bytes of data are received.

configureCallback(t,"byte",10,@readDataFcn);

Send 10 bytes of data to the echo server.

sendData = 1:10;
write(t,sendData,"uint8");

The echo server sends the binary data back to the TCP/IP client.

Pause for 1 second to allow the callback function readDataFcn to complete its operation.

pause(1);

Read binary data stored in UserData property and display it.

data = t.UserData
data = 1×10 uint8 row vector

    1    2    3    4    5    6    7    8    9   10

This data matches the data you wrote to the echo server.

Write and Read ASCII Data Using Terminator Callback Mode

Create a callback function called readASCIIFcn to read data each time a terminator is found in the data. Store the read data in the UserData property of tcpclient object. See the readASCIIFcn function at the end of this example.

Set the callback function to read terminated string data. The callback is triggered when it receives a terminator in the data.

configureCallback(t,"terminator",@readASCIIFcn);

Set the Terminator property value to "LF".

configureTerminator(t,"LF");

Send string data to the echo server using writeline. The terminator character "LF" is automatically appended to this string data.

writeline(t,"Echo this string.");

The echo server sends the ASCII data back to the TCP/IP client.

Pause for 1 second to allow the callback function readASCIIFcn to complete its operation.

pause(1);

Read ASCII data stored in UserData property and display it.

textData = t.UserData
textData = 
"Echo this string."

This data matches the data you wrote to the echo server.

Clear the Connection

Stop the echo server and clear the tcpclient object.

echotcpip("off");
clear t

Callback Functions

Callback Function to Read Binary Data

This function calls read to read BytesAvailableFcnCount number of bytes of data. This data is echoed back by the server.

function readDataFcn(src, ~)
src.UserData = read(src,src.BytesAvailableFcnCount,"uint8");
end

Callback Function to Read ASCII Data

This function calls readline to read ASCII data originally sent by the tcpclient object. The data is echoed back by the server.

function readASCIIFcn(src, ~)
src.UserData = readline(src);
end