tcpclient

Create TCP/IP client object to communicate over TCP/IP

Description

example

t = tcpclient('Address',Port) constructs a TCP/IP object, t, associated with remote host, Address, and remote port value, Port. The address can be either a remote host name or a remote IP address. The port must be a positive integer between 1 and 65535.

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

example

t = tcpclient('Address',Port,'Timeout',<timeout_value>) additionally sets a timeout value. The Timeout property specifies the waiting time to complete read and write operations in seconds, and the default is 10.

example

t = tcpclient('Address',Port,'ConnectTimeout',<connect_timeout_value>) additionally sets a connection timeout 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 default is Inf.

Examples

collapse all

Create 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 default to an IPv6 format. If the server you are connecting to is expecting an IPv4 format, it will fail. If this happens, you can connect by specifying an explicit IP address, rather than a host name.

Create the TCP/IP object t using 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

Create the TCP/IP object t and increase 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.

Create the TCP/IP object t and set the ConnectTimeout to 30 seconds.

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

   tcpclient with properties:

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

The output reflects the ConnectTimeout property change.

Input Arguments

collapse all

Remote host name or IP address for connection, specified as a character vector. Specify address as the first argument when you create the tcpclient object.

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

Data Types: char

Remote host port for connection, specified as a numeric scalar. Specify port number as the second argument when you create the tcpclient object. The Port must be a positive integer between 1 and 65535.

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

Data Types: double

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: t = tcpclient('172.28.154.231', 120, 'Timeout', 15, 'ConnectTimeout', 30)

Timeout for read/write operation specified as the comma-separated pair consisting of 'Timeout' and a positive value of type double. You can change the value either during object creation or after you create the object.

For information on how to change the timeout value after object creation, see Configure Properties for TCP/IP Communication.

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

Data Types: double

Timeout for connection to remote host specified as the comma-separated pair consisting of 'ConnectTimeout' and a positive value of type double. The property specifies the maximum time in seconds to wait for a connection request to the specified remote host to succeed or fail. You can only change the value during object creation.

Example: t = tcpclient('172.28.154.231', 4012, 'ConnectTimeout', 30)

Data Types: double

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced in R2014b