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);
创建一个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
创建一个callback function calledreadDataFcn
to read data each time the specified bytes of data are available. Store the read data in theUserData
property oftcpclient
对象。See thereadDataFcn
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 functionreadDataFcn
to complete its operation.
pause(1);
Read binary data stored inUserData
property 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 calledreadASCIIFcn
to read data each time a terminator is found in the data. Store the read data in theUserData
property oftcpclient
对象。See thereadASCIIFcn
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 theTerminator
property 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 functionreadASCIIFcn
to complete its operation.
pause(1);
Read ASCII data stored inUserData
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 thetcpclient
对象。
echotcpip("off"); cleart
Callback Functions
Callback Function to Read Binary Data
这个函数调用read
to readBytesAvailableFcnCount
number 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