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 atcpclient对象。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

创建一个TCP/IP echo server on port 4500.

echotcpip("on",4500);

创建一个tcpclientobject 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

创建一个callback function calledreadDataFcnto read data each time the specified bytes of data are available. Store the read data in theUserDataproperty oftcpclient对象。See thereadDataFcnfunction 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 functionreadDataFcnto complete its operation.

pause(1);

Read binary data stored inUserDataproperty and display it.

data = t.UserData
data =1×10 uint8 row vector1 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

创建一个callback function calledreadASCIIFcnto read data each time a terminator is found in the data. Store the read data in theUserDataproperty oftcpclient对象。See thereadASCIIFcnfunction 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 theTerminatorproperty value to"LF".

configureTerminator(t,"LF");

Send string data to the echo server usingwriteline. 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 functionreadASCIIFcnto complete its operation.

pause(1);

Read ASCII data stored inUserDataproperty 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 thetcpclient对象。

echotcpip("off"); cleart

Callback Functions

Callback Function to Read Binary Data

这个函数调用readto readBytesAvailableFcnCountnumber of bytes of data. This data is echoed back by the server.

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

Callback Function to Read ASCII Data

这个函数调用readline最初读ASCII数据sent by thetcpclient对象。The data is echoed back by the server.

functionreadASCIIFcn(src, ~) src.UserData = readline(src);end