Main Content


Find TCP/IP client connections

Since R2024a



    T = tcpclientfind finds existing TCP/IP client connections and returns an array of tcpclient objects corresponding to each connection.


    T = tcpclientfind(Name=Value) finds connections with property values matching those specified by one or more name-value arguments. For instance, T = tcpclientfind(Tag="mySensor") returns existing TCP/IP client connections whose Tag property is set to "mySensor".


    collapse all

    When you have a tcpclient connection that exists in the MATLAB® workspace or is saved as a class property or app property, the tcpclient object might not be accessible in a different function or app callback. In this case, you can use tcpclientfind to find and delete the connection.

    T = tcpclientfind
    T = 
       tcpclient with properties:
                  Address: ''
                     Port: 80
                      Tag: ""
        NumBytesAvailable: 0

    To close this connection, delete T.


    This command deletes the tcpclient object and disconnects the client. If you subsequently want to reconnect to the host, you must create a new client interface with tcpclient.

    After the deletion, calling tcpclientfind confirms that there are no existing connections.

    ans =

    Note that the variable T is still present in the workspace, but it is now an invalid handle.

    N = 
      handle to deleted tcpclient

    The variable persists after deletion of the interface because tcpclient is a handle object. (For more information about this type of object, see Handle Object Behavior.) You can use clear to remove the invalid handle from the workspace.

    clear T

    You can assign a tag to a connection and use that tag with tcpclientfind to access the connection later. Such tags are useful when you have multiple clients connections to keep track of across several functions. Tags are also useful for locating and accessing connections in app callbacks. To set the tag value, use the Tag property of tcpclient.

    Create two TCP/IP client connections, assigning values to the Tag property.

    c1 = tcpclient("",6000,Tag="Send");
    c2 = tcpclient("",10000,Timeout=2,Tag="Receive");

    Find the connection with the tag "Receive".

    T = tcpclientfind(Tag="Receive")
    T = 
      tcpclient with properties:
                  Address: ''
                     Port: 10000
                      Tag: "Receive"
        NumBytesAvailable: 0
      Show all properties, functions

    Input Arguments

    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.

    Example: tcpclientfind(Tag="Send",Port=6000) returns existing TCP/IP client connections to port 6000 whose Tag property is set to "Send".

    For tcpclientfind, you can use one or more properties of the tcpclient object as name-value arguments to specify characteristics of the connections you want to find.

    Output Arguments

    collapse all

    TCP/IP client connections, returned as a tcpclient object or an array of tcpclient objects. If you call tcpclientfind with no Name=Value pairs, T contains all existing connections. Otherwise, T contains all connections whose properties match the values you specify with Name=Value pairs.

    T is empty if:

    • There are no existing TCP/IP client connections.

    • No existing connections match the specified property values. For instance, if you specify Tag="Scope", and there is no existing connection whose Tag property is "Scope", then T is empty.

    • You try to match a property that doesn't exist in tcpclient. For instance, tcpclientfind(Speed=14400) returns an empty array, because tcpclient does not have a Speed property.

    Version History

    Introduced in R2024a

    See Also

    | |