Main Content

tcpserverfind

Find TCP/IP servers

Since R2024a

    Description

    T = tcpserverfind finds existing TCP/IP servers and returns an array of tcpserver objects corresponding to each server.

    example

    T = tcpserverfind(Name=Value) finds servers with property values matching those specified by one or more name-value arguments. For instance, T = tcpserverfind(Tag="Scope") returns existing servers whose Tag property is set to "Scope".

    example

    Examples

    collapse all

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

    T = tcpserverfind
    T = 
    
      TCPServer with properties:
    
            ServerAddress: "127.0.0.1"
               ServerPort: 6000
                Connected: 0
            ClientAddress: ""
               ClientPort: []
                      Tag: "Analyzer"
        NumBytesAvailable: 0
    

    To close this connection, delete T.

    delete(T)

    This command deletes the tcpserver object and disconnects the server. If you want to reconnect to the server, you must create a new server interface with tcpserver.

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

    tcpserverfind
    ans =
    
         []

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

    T
    T = 
    
      handle to deleted tcpserver

    The variable persists after deletion of the interface because tcpserver 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 TCP/IP server and use that tag with tcpserverfind to access the server later. Such tags are useful when you have multiple servers to keep track of across several functions. Tags are also useful for locating and accessing servers in app callbacks. To set the tag value, use the Tag property of tcpcserver.

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

    s1 = tcpserver("localhost",4000,Tag="Scope");
    s2 = tcpserver("localhost",6000,Tag="Analyzer");

    Find the server with the tag "Analyzer".

    T = tcpserverfind(Tag="Analyzer")
    T = 
    
      TCPServer with properties:
    
            ServerAddress: "127.0.0.1"
               ServerPort: 6000
                Connected: 0
            ClientAddress: ""
               ClientPort: []
                      Tag: "Analyzer"
        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: tcpserverfind(Tag="Scope",ServerPort=6000) returns existing servers using port 6000 whose Tag property is set to "Scope".

    For tcpserverfind, you can use one or more properties of the tcpserver object as name-value arguments to specify characteristics of the servers you want to find.

    Output Arguments

    collapse all

    TCP/IP servers, returned as a tcpserver object or an array of tcpserver objects. If you call tcpserverfind with no name-value arguments, T contains all existing TCP/IP servers. Otherwise, T contains all servers whose properties match the values you specify with name-value arguments.

    T is empty if:

    • There are no existing TCP/IP servers.

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

    • You try to match a property that the tcpserver object does not have. For instance, tcpserverfind(Speed=14400) returns an empty array because tcpserver does not have a Speed property.

    Version History

    Introduced in R2024a

    See Also

    | |