2

I have 3 cameras whose MAC and IP addresses are unknown to me, and I want to connect to them.

I've connected them to my computer with a UTP cable. Now what I have to do is to find their IP addresses in order to establish a connection. In the camera's API, it states that you can use TCP/IP to get video stream from the camera and to use mDNS to find their IP addresses.

How can I find the IP addresses in c#?

(NOTE: no router in need and "arp -a" command did not work)

4

2 回答 2

2

发送这个 mDNS 数据包似乎适用于我的具有 Raspberry Pi 的应用程序。我认为 IP/端口特定于 ZeroConf/Bonjour,它似乎被广泛使用,因此它可能适用于那些相机。

public void sendDiscoveryQuery(string local_dhcp_ip_address)
{
    // multicast UDP-based mDNS-packet for discovering IP addresses

    System.Net.Sockets.Socket socket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);

    socket.Bind(new IPEndPoint(IPAddress.Parse(local_dhcp_ip_address), 52634));
    IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("224.0.0.251"), 5353);

    List<byte> bytes = new List<byte>();

    bytes.AddRange(new byte[] { 0x0, 0x0 });  // transaction id (ignored)
    bytes.AddRange(new byte[] { 0x1, 0x0 });  // standard query
    bytes.AddRange(new byte[] { 0x0, 0x1 });  // questions
    bytes.AddRange(new byte[] { 0x0, 0x0 });  // answer RRs
    bytes.AddRange(new byte[] { 0x0, 0x0 });  // authority RRs
    bytes.AddRange(new byte[] { 0x0, 0x0 });  // additional RRs
    bytes.AddRange(new byte[] { 0x05, 0x5f, 0x68, 0x74, 0x74, 0x70, 0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01 });  // _http._tcp.local: type PTR, class IN, "QM" question

    socket.SendTo(bytes.ToArray(), endpoint);
}

“local_dhcp_ip_address”是您计算机网卡的 IP 地址。下面是此交互的wireshark 跟踪。该设备直接响应您的计算机。

在此处输入图像描述

此时 ARP -a 应该可以工作。您也可以使用GetIpNetTable

于 2019-07-15T21:01:47.653 回答
0

在某些设备上,MAC 地址印在标签上,可能在背面。据我了解,它们至少需要在 arp 缓存中一次。但此缓存仅存储 5 (?) 个条目,因此您可能需要在连接设备或运行配置工具之前刷新或清除它。

于 2012-04-29T07:42:34.140 回答