Create a TCP/IP Connection

The 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, you first create a TCP/IP object using the tcpclient function. The syntax is:

<objname> = 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

This example creates the TCP/IP object t using the host address shown and port of 80.

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

t = 

   tcpclient with properties:

          Address: 'www.mathworks.com'
             Port: 80
          Timeout: 10
   BytesAvailable: 0
   ConnectTimeout: Inf

Note

When connecting using a host name, such as a specified web address or 'localhost', the IP address will be resolved according to the configuration of your network interface. This may result in an IPv4 address or an IPv6 address. If your TCP/IP server expects the incoming connections to be of a certain type of address, for example IPv4 address only, you may be required to use the explicit IP address, instead of the host name, when creating the client.

Create Object Using IP Address

This example creates the TCP/IP object tusing the IP address shown and port of 4012.

t = tcpclient('172.28.154.231', 4012)

t = 

   tcpclient with properties:

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

Set the Timeout Property

You can create the object using a name-value pair to set the Timeout value. The Timeout property specifies the waiting time to complete read and write operations in seconds, and the default is 10. You can change the value either during object creation or after you create the object.

This example creates a TCP/IP object, but 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.

Set the Connect Timeout Property

You can create the 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 specifies the ConnectTimeout as 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.

Note

If an invalid address or port is specified or the connection to the server cannot be established, the object is not created.