在Bonjour 1.0中调用GetAddrInfo(....)时的主机名是什么

时间:2014-01-11 17:27:16

标签: c# .net bonjour

我使用Bonjour 1.0(。NET)通过我的本地家庭网络扫描了regtype = "_http._tcp"的服务:

enter image description here

 DNSSDService service = new DNSSDService();
            DNSSDEventManager eventManager = new DNSSDEventManager();
            eventManager.ServiceFound += new _IDNSSDEvents_ServiceFoundEventHandler(eventManager_ServiceFound);
            DNSSDService browse = service.Browse(0, 0, "_http._tcp", null, eventManager);

当我找到该服务时,方法:static void eventManager_ServiceFound(DNSSDService browser, DNSSDFlags flags, uint ifIndex, string serviceName, string regtype, string domain)被调用。

然后在该方法中,我想使用方法:DNSSDService GetAddrInfo(DNSSDFlags glags, uint ifIndex, DNSSDAddressFamily addressFamily, string hostname , DNSSDEventManager eventManager)来了解服务的IP地址和端口。

问题是我不知道 string hostname 应该是什么,因此我没有得到任何结果:**请查看:这里有什么?在代码**

 static void eventManager_ServiceFound(DNSSDService browser, DNSSDFlags flags, uint ifIndex, string serviceName, string regtype, string domain) {
            Console.WriteLine("browser: " + browser + "\nDNSSDFlags " +  flags+ "\nifIndex " + ifIndex +"\nserviceName: " + serviceName + "\nregtype: " +regtype+ "\ndomain: "+ domain);
            DNSSDEventManager eventManager = new DNSSDEventManager();
            eventManager.AddressFound += new _IDNSSDEvents_AddressFoundEventHandler(eventManager_AddressFound);
            DNSSDAddressFamily family = new DNSSDAddressFamily();
            browser.GetAddrInfo(flags, ifIndex, family, ???WHAT HERE???, eventManager);
        }

        private static void eventManager_AddressFound(DNSSDService service, DNSSDFlags flags, uint ifIndex, string hostname, DNSSDAddressFamily addressFamily, string address, uint ttl) {
            Console.WriteLine("----------------------------------------");
            Console.WriteLine("FOUND THE ADDRESS");
            Console.WriteLine("----------------------------------------");
        }

输出:

browser: System.__ComObject
DNSSDFlags 2
ifIndex 32
serviceName: AXIS M1011-W - 00408CBEEAE5
regtype: _http._tcp.
domain: local.

完整代码:

using Bonjour;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
    static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            DNSSDService service = new DNSSDService();
            DNSSDEventManager eventManager = new DNSSDEventManager();
            eventManager.ServiceFound += new _IDNSSDEvents_ServiceFoundEventHandler(eventManager_ServiceFound);
         //   DNSSDService browse = service.Browse(0, 0, "_axis-video._tcp", null, eventManager);
            DNSSDService browse = service.Browse(0, 0, "_http._tcp", null, eventManager);


            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
        static void eventManager_ServiceFound(DNSSDService browser, DNSSDFlags flags, uint ifIndex, string serviceName, string regtype, string domain) {
            Console.WriteLine("browser: " + browser + "\nDNSSDFlags " +  flags+ "\nifIndex " + ifIndex +"\nserviceName: " + serviceName + "\nregtype: " +regtype+ "\ndomain: "+ domain);
            DNSSDEventManager eventManager = new DNSSDEventManager();
            eventManager.AddressFound += new _IDNSSDEvents_AddressFoundEventHandler(eventManager_AddressFound);
            DNSSDAddressFamily family = new DNSSDAddressFamily();
            browser.GetAddrInfo(flags, ifIndex, family, ?????WHAT HERE?????, eventManager);
        }

        private static void eventManager_AddressFound(DNSSDService service, DNSSDFlags flags, uint ifIndex, string hostname, DNSSDAddressFamily addressFamily, string address, uint ttl) {
            Console.WriteLine("----------------------------------------");
            Console.WriteLine("FOUND ADDRESS");
            Console.WriteLine("----------------------------------------");
        }



    }
}

1 个答案:

答案 0 :(得分:1)

我首先检查的是与_http._tcp记录关联的TXT记录。从屏幕截图中选择_axis-video._tcp,它会显示与之关联的macaddress。许多_http._tcp条目记录了获取它所需的URL,或者需要的端口/ ipaddress。我先挖掘它。

否则您可能必须将macaddress解析为ipaddress。我用谷歌搜索了http://www.mostthingsweb.com/2011/11/reading-arp-entries-with-c/