Main Content

udpportfind

Find UDP socket connections

Since R2024a

    Description

    U = udpportfind finds existing UDP socket connections and returns an array of udpport objects corresponding to each connection.

    example

    U = udpportfind(Name=Value) finds connections with property values matching those specified by one or more name-value arguments. For instance, U = udpportfind(Tag="Sender") returns existing UDP socket connections whose Tag property is set to "Sender".

    example

    Examples

    collapse all

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

    U = udpportfind
    U = 
    
      UDPPort with properties:
    
             IPAddressVersion: "IPV4"
                    LocalHost: "0.0.0.0"
                    LocalPort: 3030
                          Tag: "Receiver"
        NumDatagramsAvailable: 0
    

    To close this connection, delete U.

    delete(U)

    This command deletes the udpport object and disconnects the device. If you want to reconnect to the device, you must create a new connection with udpport.

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

    udpportfind
    ans =
    
         []

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

    U
    U = 
    
      handle to deleted UDPPort

    The variable persists after deletion of the interface because udpport 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 U

    You can assign a tag to a connection and use that tag with udpportfind to access the connection later. Such tags are useful when you have multiple UDP socket 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 udpport.

    Create two UDP socket connections, assigning values to the Tag property.

    u1 = udpport(Tag="Sender");
    u2 = udpport("datagram",LocalPort=3030,Tag="Receiver");

    Find connections with the tag "Receiver".

    U = udpportfind(Tag="Receiver")
    U = 
    
      UDPPort with properties:
    
             IPAddressVersion: "IPV4"
                    LocalHost: "0.0.0.0"
                    LocalPort: 3030
                          Tag: "Receiver"
        NumDatagramsAvailable: 0
    

    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: udpportfind(Tag="Receiver",EnablePortSharing=1) returns existing UDP socket connections whose Tag property is set to "Receiver" and have port sharing enabled.

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

    Output Arguments

    collapse all

    UDP socket connections, returned as a UDP interface object (see udpport) or an array of such objects. If you call udpportfind with no name-value arguments, U contains all existing connections. Otherwise, U contains all connections whose properties match the values you specify with name-value arguments.

    U is empty if:

    • There are no existing UDP socket connections.

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

    • You try to match a property that the udpport object does not have. For instance, udpportfind(Address="198.51.100.255") returns an empty array because udpport does not have an Address property.

    Version History

    Introduced in R2024a

    See Also