使用Python link,我可以发布服务集,该服务集是用户在控制中心中选择设备时要连接的IP地址。
以下是Python代码。现在在C#中使用NSNetService,我试图实现同样的目的。
发布正常,但是将IP地址socket.inet_aton(“ 10.100.X.XX”)添加到NSNet服务中,我无法在C#中实现。请发布一些准则以实现。如何将IP地址添加到NSNETService以在NSNetBrowser中显示该IP地址。

_testService = new NSNetService("_airplay._tcp.local.", "_testservice._tcp", "Test Service", 7000); _testService.Published += delegate (object sender, EventArgs e) { NSNetService ns = sender as NSNetService; Console.WriteLine("Bonjour published service '{0}'", ns.Name); }; _testService.WillPublish += delegate (object sender, EventArgs e) { NSNetService ns = sender as NSNetService; Console.WriteLine("Bonjour will publish service '{0}'", ns.Name); }; _testService.PublishFailure += delegate (object sender, NSNetServiceErrorEventArgs e) { NSNetService ns = sender as NSNetService; Console.WriteLine("Bonjour failed to publish service '{0}'", ns.Name); };这个


import logging
import socket
import sys
from time import sleep
 
from zeroconf import ServiceInfo, Zeroconf
 
if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    if len(sys.argv) > 1:
        assert sys.argv[1:] == ['--debug']
        logging.getLogger('zeroconf').setLevel(logging.DEBUG)
 
    airplay_desc = {'deviceid': '00:05:a6:16:45:4b',
                    'features': '0xA7FFFF7,0xE',
                    'flags':    '0x4',
                    'model':    'AppleTV5,3',
                    'pi':       '6b448552-85ce-4143-a896-e28d12e8a0ab',
                    'pk':       'F381DC574DEAF9C70B75297755BC7C7C35BB1D0DB500258F3AB46B5FE7C7355B',
                    'srcvers':  '220.68',
                    'vv':       '2'}
 
    airplay_info = ServiceInfo("_airplay._tcp.local.",
                       "SLP._airplay._tcp.local.",
                       socket.inet_aton("10.100.251.193"), 7000, 0, 0,
                       airplay_desc)
 
    raop_desc = {'am': 'AppleTV5,3',
                 'ch':'2',
                 'cn':'0,1,2,3',
                 'da':'true',
                 'ek':'1',
                 'et':'0,3,5',
                 'md':'0,1,2',
                 'pw':'false',
                 'sm':'false',
                 'sr':'44100',
                 'ss':'16',
                 'sv':'false',
                 'tp':'UDP',
                 'tx':'vers=1',
                 'vn':'65537',
                 'vs':'220.68',
                 'sf':'0x4',
                 'ft':'0xA7FFFF7,0xE',
                 'pk':'F381DC574DEAF9C70B75297755BC7C7C35BB1D0DB500258F3AB46B5FE7C7355B',
                 'vv':'2'}
 
    raop_info = ServiceInfo("_raop._tcp.local.",
                       "0005A616454B@SLP._raop._tcp.local.",
                       socket.inet_aton("10.100.X.XX"), 7000, 0, 0,
                       raop_desc)
 
    zeroconf = Zeroconf()
    print("Registration of a service, press Ctrl-C to exit...")
    zeroconf.register_service(airplay_info)
    zeroconf.register_service(raop_info, allow_name_change=True)
 
    try:
        while True:
            sleep(0.1)
    except KeyboardInterrupt:
        pass
    finally:
        print("Unregistering...")
        zeroconf.unregister_service(airplay_info)
        zeroconf.close()

最佳答案

为遇到此问题的任何人创建了一个仓库。仅供参考:使用NSNetService无法实现目标,但使用net-mdns可以达到https://github.com/Raj123456788/ZeroConfiOS

关于c# - NSNetService使用C#分配IP地址,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58034784/

10-16 18:28