MATLAB 帮助中心
查找 TCP/IP 客户端连接
自 R2024a 起
T = tcpclientfind
T = tcpclientfind(Name=Value)
T = tcpclientfind 查找现有 TCP/IP 客户端连接,并返回与每个连接对应的 tcpclient 对象的数组。
T
tcpclient
示例
T = tcpclientfind(Name=Value) 查找其属性值与一个或多个名称-值参量指定的属性值匹配的连接。例如,T = tcpclientfind(Tag="mySensor") 返回其 Tag 属性设置为 "mySensor" 的现有 TCP/IP 客户端连接。
Name=Value
T = tcpclientfind(Tag="mySensor")
Tag
"mySensor"
全部折叠
当一个 tcpclient 连接存在于 MATLAB® 工作区中或保存为类属性或 App 属性时,可能无法在不同函数或 App 回调中访问 tcpclient 对象。在这种情况下,您可以使用 tcpclientfind 查找和删除该连接。
tcpclientfind
T = tcpclient with properties: Address: '198.51.100.255' Port: 80 Tag: "" NumBytesAvailable: 0
要关闭此连接,请删除 T。
delete(T)
此命令删除 tcpclient 对象并断开与客户端的连接。如果您要重新连接到主机,您必须使用 tcpclient 创建一个新客户端接口。
在删除后,调用 tcpclientfind 确认没有现有连接。
ans = []
请注意,变量 T 仍存在于工作区中,但它现在是无效句柄。
T = handle to deleted tcpclient
由于 tcpclient 是句柄对象,因此该变量在删除接口后仍存在。(有关这种类型的对象的详细信息,请参阅句柄对象行为。)您可以使用 clear 从工作区中删除无效的句柄。
clear
clear T
您可以为连接分配一个标记,并稍后将该标记与 tcpclientfind 一起使用来访问该连接。当您需要在多个函数之间跟踪多个客户端连接时,这种标记非常有用。标记对于定位和访问 App 回调中的连接也很有用。要设置标记值,请使用 tcpclient 的 Tag 属性。
创建两个 TCP/IP 客户端连接,为 Tag 属性赋值。
c1 = tcpclient("192.168.1.2",6000,Tag="Send"); c2 = tcpclient("192.168.1.2",10000,Timeout=2,Tag="Receive");
查找具有标记 "Receive" 的连接。
"Receive"
T = tcpclientfind(Tag="Receive")
T = tcpclient with properties: Address: '192.168.1.2' Port: 10000 Tag: "Receive" NumBytesAvailable: 0 Show all properties, functions
将可选参量对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但对各个参量对组的顺序没有要求。
Name1=Value1,...,NameN=ValueN
Name
Value
示例: tcpclientfind(Tag="Send",Port=6000) 返回到端口 6000 且其 Tag 属性设置为 "Send" 的现有 TCP/IP 客户端连接。
tcpclientfind(Tag="Send",Port=6000)
"Send"
对于 tcpclientfind,您可以使用 tcpclient 对象的一个或多个属性作为名称-值参量来指定要查找的连接的特性。
TCP/IP 客户端连接,以 tcpclient 对象或 tcpclient 对象数组形式返回。如果您调用不带名称-值参量的 tcpclientfind,则 T 包含所有现有连接。否则,T 包含其属性与您使用名称-值参量指定的值匹配的所有连接。
如果满足以下条件,则 T 为空:
不存在现有 TCP/IP 客户端连接。
不存在与指定的属性值匹配的现有连接。例如,如果您指定 Tag="Scope" 并且不存在其 Tag 属性为 "Scope" 的现有连接,则 T 为空。
Tag="Scope"
"Scope"
您尝试匹配 tcpclient 对象没有的属性。例如,tcpclientfind(Speed=14400) 返回空数组,因为 tcpclient 没有 Speed 属性。
tcpclientfind(Speed=14400)
Speed
在 R2024a 中推出
tcpclient | delete | clear
delete
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
欧洲
亚太
联系您当地的办事处