%% Write and Read uint8 Data from Remote Host
% Create a TCP/IP client connection called t, connecting to a TCP/IP echo server with port 4000. To do so, you must have an echotcpip server running on port 4000.

echotcpip("on",4000)
t = tcpclient("localhost",4000)

%
% The write function synchronously writes data to the remote host connected to t. First specify the data and then write the data. The function suspends MATLAB execution until the specified number of values is written to the remote host.
% Assign 10 bytes of uint8 data to the variable data.

data = uint8(1:10)

% View the data.

whos data

% Write data to the echo server.

write(t,data)

% Confirm the success of the writing operation by viewing the NumBytesAvailable property.

t.NumBytesAvailable

%
% Since the client is connected to an echo server, the data you write to the server is returned to the client. Read all the bytes of data available.

read(t)

% Using the read function with no arguments reads all available bytes of data from t connected to the remote host and returns the data. The number of values read is determined by the NumBytesAvailable property, which is the number of bytes available in the input buffer.
%
% Close the connection between the TCP/IP client and the remote host by clearing the object. Turn off the echotcpip server.

clear t
echotcpip("off")

%
% Copyright 2020 The MathWorks, Inc.