Main Content

tcpip

(To be removed) Create TCPIP object

tcpip will be removed. Use tcpclient or tcpserver instead. For more information on updating your code, see Version History.

Description

t = tcpip(RemoteHost) creates a TCPIP object, t, associated with remote host RemoteHost and the default remote port value of 80.

When the TCPIP object is created, its Status property value is closed. When the object is connected to the host with the fopen function, the Status property is configured to open.

The default local host in multihome hosts is the system default. The LocalPort property defaults to a value of [], which allows any free local port to be used. LocalPort is assigned when fopen is issued.

t = tcpip(RemoteHost,RemotePort) creates a TCPIP object with the specified remote port value RemotePort.

t = tcpip(___,Name,Value) creates a TCPIP object with the specified optional name-value pairs. If an invalid property name or property value is specified, the object is not created.

example

Examples

collapse all

Use a TCPIP object to write to an echo server and read back the message.

Start a TCP/IP echo server and create a TCPIP object.

echotcpip('on',4012)
t = tcpip('localhost',4012);

Connect the TCPIP object to the host.

fopen(t)

Write to the host and read from the host.

fwrite(t,65:74)
A = fread(t,10)
A =

    65
    66
    67
    68
    69
    70
    71
    72
    73
    74

Disconnect the TCPIP object from the host and stop the echo server.

fclose(t)
echotcpip('off')

Input Arguments

collapse all

Remote host ID, specified as a character vector or string, identifying IP address or host name.

Example: '127.0.0.1'

Data Types: char | string

Port on remote host, specified as a numeric integer value from 1 to 65535.

Example: 8001

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: 'NetworkRole','server'

Network role for TCPIP object, specified as 'client' or 'server'. For example, t = tcpip('localhost',30000,'NetworkRole','server') creates a TCPIP object, t, that is an interface for a server socket.

Example: 'server'

Data Types: char | string

Name of interface object, specified as a character vector or string.

Example: 'TCPdev1'

Data Types: char | string

Time limit in seconds for communication, specified as a numeric value. The default is 10 seconds.

Example: 60

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

collapse all

TCPIP interface, returned as an interface object.

Version History

Introduced before R2006a

expand all

R2022a: Warns

tcpip will be removed. Use tcpclient or tcpserver instead.

This example shows how to create a TCP/IP client using the recommended functionality.

FunctionalityUse This Instead
t = tcpip("127.0.0.1",3030,"NetworkRole","client");
fopen(t)
t = tcpclient("127.0.0.1",3030);

This example shows how to create a TCP/IP server using the recommended functionality.

FunctionalityUse This Instead
t = tcpip("192.168.2.15",3030,"NetworkRole","server");
fopen(t)

This binds to host "0.0.0.0" (internally) and port 3030 and only accepts client connections coming from "192.168.2.15". MATLAB® is blocked until a client connection is established.

t = tcpserver("0.0.0.0",3030);

This binds to "0.0.0.0" and port 3030. "0.0.0.0" means that it accepts any incoming client connection requests on port 3030. MATLAB is not blocked.

The recommended interfaces have additional capabilities and improved performance. See Transition Your Code to tcpclient Interface or Transition Your Code to tcpserver Interface for more information about using the recommended functionality.

See Also

Functions