海康SADPTool代码实现

SADPTool原理:

239.255.255.250:37020发送基于ONVIF协议的 udp 组播,设备会监听 239.255.255.250:37020,收到指令,会向 发送组播的机器,返回设备信息,也会向239.255.255.250:37020 发送 设备信息的 组播。

发送 报文(Uuid是Guid,每次都生成一个新的)

<?xml version="1.0" encoding="utf-8"?><Probe><Uuid>12D17626-0DE1-48DB-97E3-40B106467932</Uuid><Types>inquiry</Types></Probe>

返回报文

<?xml version="1.0" encoding="UTF-8" ?> <ProbeMatch> <Uuid>12D17626-0DE1-48DB-97E3-40B106467932</Uuid> <Types>inquiry</Types> <DeviceType>139991</DeviceType> <DeviceDescription>CS-C6TC-32WFR</DeviceDescription> <DeviceSN>CS-C6TC-32WFR0120170327CCCH738231995</DeviceSN> <CommandPort>8000</CommandPort> <HttpPort>80</HttpPort> <MAC>54-c4-15-9a-38-72</MAC> <IPv4Address>172.20.20.100</IPv4Address> <IPv4SubnetMask>255.255.255.0</IPv4SubnetMask> <IPv4Gateway>172.20.20.254</IPv4Gateway> <DHCP>false</DHCP> <AnalogChannelNum>0</AnalogChannelNum> <DigitalChannelNum>1</DigitalChannelNum> <SoftwareVersion>V5.2.3build 180804</SoftwareVersion> <DSPVersion>V1.0 build 180731</DSPVersion> <BootTime>2018-10-18 21:30:10</BootTime> <OEMInfo>N/A</OEMInfo> <Activated>true</Activated> <PasswordResetAbility>false</PasswordResetAbility> </ProbeMatch>

实现代码:

using System.Net.Sockets; using System.Net; using System.Text; using System; using System.Threading.Tasks; namespace ConsoleApp { class Program { public static void Main(string[] args) { var localIp = "192.168.51.215";//本地ip UdpClient client = new UdpClient(new IPEndPoint(IPAddress.Parse(localIp), 37020)); Task.Factory.StartNew(() => { IPEndPoint mult = null; while (true) { byte[] buf = client.Receive(ref mult); string msg = Encoding.ASCII.GetString(buf); Console.WriteLine(msg); } }); var xml = $"<?xml version=\"1.0\" encoding=\"utf-8\"?><Probe><Uuid>{Guid.NewGuid().ToString().ToUpper()}</Uuid><Types>inquiry</Types></Probe>"; var bytes = Encoding.ASCII.GetBytes(xml); IPEndPoint multicast = new IPEndPoint(IPAddress.Parse("239.255.255.250"), 37020); client.Send(bytes, bytes.Length, multicast); Console.ReadKey(); } } }
鲸之声为您拼命加载中...