You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ONVIF协议多播搜索IP摄像头不稳定问题排查求助

ONVIF多播搜索摄像头不稳定问题排查

问题核心

你的代码通过ONVIF多播发送Probe消息搜索摄像头,但仅能偶尔搜到全部设备,而ONVIF Device Manager可稳定获取30台设备,问题主要出在SOAP格式、接收逻辑、多播配置这几个环节。

具体问题点及修复方案

1. SOAP消息格式错误(最核心问题)

CreateSoapFindCamera方法里的<w:Action>标签存在拼写错误:

<w:Actiona:mustUnderstand=""true"">http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe</w:Action>

这里的Actiona是笔误,且未正确引用命名空间前缀,设备收到格式错误的SOAP消息会直接丢弃。正确写法如下:

<w:Action e:mustUnderstand=""true"">http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe</w:Action>

修复后的完整方法:

private static string CreateSoapFindCamera()
{
    string messageId = "urn:uuid:" + Guid.NewGuid().ToString();
    const string soap = @"
    <?xml version=""1.0"" encoding=""UTF-8""?>
    <e:Envelope 
    xmlns:e=""http://www.w3.org/2003/05/soap-envelope""
    xmlns:w=""http://schemas.xmlsoap.org/ws/2004/08/addressing"" 
    xmlns:d=""http://schemas.xmlsoap.org/ws/2005/04/discovery"" 
    xmlns:dn=""http://www.onvif.org/ver10/network/wsdl"">
        <e:Header>
        <w:MessageID>{0}</w:MessageID>
        <w:To e:mustUnderstand=""true"">urn:schemas-xmlsoap-org:ws:2005:04:discovery</w:To>
        <w:Action e:mustUnderstand=""true"">http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe</w:Action>
        </e:Header>
        <e:Body>
        <d:Probe>
          <d:Types>dn:NetworkVideoTransmitter</d:Types>
        </d:Probe>
      </e:Body>
    </e:Envelope>";
    var result = string.Format(soap, messageId);
    return result;
}

2. 接收逻辑存在丢包风险

当前通过client.Available > 0判断是否有数据,再调用ReceiveAsync,但30台设备同时回复时,判断和接收之间的间隙可能导致数据丢失。

修复方案:
取消Available判断,用CancellationTokenSource实现超时,直接异步等待接收:

private static async Task<List<string>> GetSoapResponsesFromCamerasAsync1(int CloseTime)
{
    var result = new List<string>();
    using (UdpClient client = new UdpClient())
    {
        System.Net.IPAddress multicastAddress = System.Net.IPAddress.Parse("239.255.255.250");
        IPEndPoint localEndpoint = new IPEndPoint(System.Net.IPAddress.Any, 0);
        IPEndPoint multicastEndpoint = new IPEndPoint(multicastAddress, 3702);
        client.ExclusiveAddressUse = false;
        client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
        client.Client.Bind(localEndpoint);
        client.JoinMulticastGroup(multicastAddress);

        try
        {
            var soapMessage = Main.util.GetBytes(CreateSoapFindCamera());
            await client.SendAsync(soapMessage, soapMessage.Length, multicastEndpoint);

            // 用取消令牌实现精准超时
            using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(CloseTime)))
            {
                while (!cts.Token.IsCancellationRequested)
                {
                    try
                    {
                        var receiveResult = await client.ReceiveAsync(cts.Token);
                        var text = Main.util.GetText(receiveResult.Buffer);
                        result.Add(text);
                    }
                    catch (OperationCanceledException)
                    {
                        // 超时触发,退出循环
                        break;
                    }
                }
            }
        }
        catch (Exception exception) { Console.WriteLine(exception.Message); }
        finally { client.DropMulticastGroup(multicastAddress); }
     }
     return result;
}

3. 多播配置优化

当前绑定IPAddress.Any可能导致消息走非预期网卡,部分设备的回复无法被接收。可以指定本地网卡IP绑定:

// 替换为你的实际网卡IP,例如192.168.1.100
var localIp = System.Net.IPAddress.Parse("192.168.1.100");
IPEndPoint localEndpoint = new IPEndPoint(localIp, 0);
client.Client.Bind(localEndpoint);
client.JoinMulticastGroup(multicastAddress, localIp);

4. 增加消息重发逻辑

ONVIF Device Manager会重复发送Probe消息确保覆盖所有设备,你可以在代码中增加多次发送:

// 重复发送3次,间隔500ms
for (int i = 0; i < 3; i++)
{
    await client.SendAsync(soapMessage, soapMessage.Length, multicastEndpoint);
    await Task.Delay(500);
}

总结

优先修复SOAP格式错误,这是设备不响应的核心原因;再优化接收逻辑避免丢包;最后通过指定网卡、重复发送进一步提升稳定性。

内容的提问来源于stack exchange,提问作者정글은엘리스

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 14:17:12