NSNetService Assign IP Address using C#

久未见 提交于 2019-12-25 00:37:33

问题


Using Python link I am able to publish a service set the IP address that I want to connect to when user selects the device in control Center. Below is the Code for Python. Now using NSNetService in C# I am trying to achieve same thing. Publish is working but adding the IP address socket.inet_aton("10.100.X.XX") To NSNet service I am unable to achieve that in C#. Please post some guidelines to achieve. How can I add the IP address to the NSNETService to show that IP address in NSNetBrowser.

_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); }; this

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()

回答1:


Created a repo for anyone who encounters this issue. FYI: Unable to achieve the goal with NSNetService but able to with net-mdns is https://github.com/Raj123456788/ZeroConfiOS



来源:https://stackoverflow.com/questions/58034784/nsnetservice-assign-ip-address-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!