Create TCP/IP Client and Configure Settings
MATLAB® TCP/IP client support lets you connect to remote hosts or hardware from MATLAB for reading and writing data. The typical workflow is:
Create a TCP/IP connection to a server or hardware.
Configure the connection if necessary.
Perform read and write operations.
Clear and close the connection.
To communicate over the TCP/IP interface, first create a tcpclient
object.
t = tcpclient(address,port);
The address can be either a remote host name or a remote IP address. In both cases, the port must be a positive integer between 1 and 65535.
Create Object Using Host Name
Create the TCP/IP object t
using the host address shown and port
80
.
t = tcpclient("www.mathworks.com",80)
t = tcpclient with properties: Address: 'www.mathworks.com' Port: 80 NumBytesAvailable: 0 Show all properties, functions
When you connect using a host name, such as a specified web address or
'localhost'
, the IP address defaults to IPv6 format. If the server
you are connecting to is expecting IPv4 format, connection fails. For IPv4, you can create
a connection by specifying an explicit IP address rather than a host name.
Create Object Using IP Address
Create the TCP/IP object t
using the IP address shown and port
80
.
t = tcpclient("144.212.130.17",80)
t = tcpclient with properties: Address: '144.212.130.17' Port: 80 NumBytesAvailable: 0 Show all properties, functions
Set Timeout Property
Create the object and use a name-value pair argument to set the
Timeout
value. The Timeout
parameter specifies the
waiting time to complete read and write operations in seconds, and the default value is
10
. You can change the value either during object creation or after
you create the object.
Create a TCP/IP object with a timeout of 20
seconds.
t = tcpclient("144.212.130.17",80,"Timeout",20)
t = tcpclient with properties: Address: '144.212.130.17' Port: 80 NumBytesAvailable: 0 Show all properties, functions
View the Timeout
property.
t.Timeout
ans = 20
The output reflects the Timeout
property change.
Set Connect Timeout Property
Create the object and use a name-value pair argument to set the
ConnectTimeout
value. The ConnectTimeout
parameter
specifies the maximum time in seconds to wait for a connection request to the specified
remote host to succeed or fail. The value must be greater than or equal to 1. If you do not
specify ConnectTimeout
, it has the default value of
Inf
. You can specify the value only during object creation.
Create a TCP/IP object and specify ConnectTimeout
as
10
seconds.
t = tcpclient("144.212.130.17",80,"ConnectTimeout",10)
t = tcpclient with properties: Address: '144.212.130.17' Port: 80 NumBytesAvailable: 0 Show all properties, functions
View the ConnectTimeout
property.
t.ConnectTimeout
ans = 10
The output reflects the ConnectTimeout
property change.
Note
If you specify an invalid address or port or the connection to the server cannot be established, the object is not created.
Set Transfer Delay Property
Create the object and use a name-value pair argument to set the
EnableTransferDelay
value. The EnableTransferDelay
parameter specifies whether Nagle's algorithm is on or off. If transfer delay is enabled,
the client collects small segments of outstanding data and sends them in a single packet
when acknowledgement (ACK) arrives from the server. If transfer delay is disabled, the
client immediately sends data to the network. If you do not specify
EnableTransferDelay
, it is true
by default. You
can specify the value only during object creation.
Create a TCP/IP object with transfer delay disabled.
t = tcpclient("144.212.130.17",80,"EnableTransferDelay",false)
t = tcpclient with properties: Address: '144.212.130.17' Port: 80 NumBytesAvailable: 0 Show all properties, functions
View the EnableTransferDelay
property.
t.EnableTransferDelay
ans = logical 0
The output reflects the EnableTransferDelay
property change.
View TCP/IP Object Properties
After you create a tcpclient
object, you can view a full list of
properties and their values. Click properties
in the
tcpclient
output.
t = tcpclient("www.mathworks.com",80)
t = tcpclient with properties: Address: 'www.mathworks.com' Port: 80 NumBytesAvailable: 0 Show all properties, functions Address: 'www.mathworks.com' Port: 80 NumBytesAvailable: 0 ConnectTimeout: Inf Timeout: 10 ByteOrder: "little-endian" Terminator: "LF" BytesAvailableFcnMode: "off" BytesAvailableFcnCount: 64 BytesAvailableFcn: [] NumBytesWritten: 0 EnableTransferDelay: 1 ErrorOccurredFcn: [] UserData: []
For more information about how to configure these properties, see Properties.
You can use the configureTerminator
and configureCallback
functions to configure certain
properties.