Configure Properties for TCP/IP Communication

The tcpclient object has the following properties.

PropertyDescription
AddressRemote host name or IP address for connection. Specify address as the first argument when you create the tcpclient object. In this example Address is '172.28.154.231'.

t = tcpclient('172.28.154.231', 4012)

PortRemote host port for connection. Specify port number as the second argument when you create the tcpclient object. The Port must be a positive integer between 1 and 65535. In this example Port is 4012.

t = tcpclient('www.mathworks.com', 4012)

BytesAvailableRead-only property that returns the number of bytes available in the input buffer.
TimeoutWaiting time in seconds to complete read and write operations, specified as a positive value of type double. The default is 10. You can change the value either during object creation, or after you create the object.
ConnectTimeoutMaximum time in seconds to wait for a connection request to the specified remote host to succeed or fail, specified as a positive value of type double. If not specified, the default value is Inf. You can change the value only during object creation.

Setting the Timeout

The default value for Timeout is 10 seconds. You can change the value either during object creation, or after you create the object.

You can optionally create the tcpclient object using a name-value pair to set the Timeout value.

This example creates the TCP/IP object and increases the Timeout to 20 seconds.

t = tcpclient('172.28.154.231', 4012, 'Timeout', 20)

t = 

   tcpclient with properties:

          Address: '172.28.154.231'
             Port: 4012
          Timeout: 20
   BytesAvailable: 0
   ConnectTimeout: Inf

The output reflects the Timeout property change from the default of 10 seconds to 20 seconds.

You can also change it anytime by setting the property value using this syntax.

<object_name>.<property_name> = <property_value>

This example using the same object named t increases the Timeout to 30 seconds.

t.Timeout = 30

Setting the Connect Timeout

You can create the tcpclient object using a name-value pair to set the ConnectTimeout value. The ConnectTimeout property 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 not specified, the default value of ConnectionTimeout is Inf. You can change the value only during object creation.

This example creates a TCP/IP object, but changes the ConnectTimeout to 10 seconds.

t = tcpclient('172.28.154.231', 4012, 'ConnectTimeout', 10)

t = 

   tcpclient with properties:

          Address: '172.28.154.231'
             Port: 4012
          Timeout: 10
   BytesAvailable: 0
   ConnectTimeout: 10

The output reflects the ConnectTimeout property change.