本文介绍了NSNetService使用C#分配IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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); };这个

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

推荐答案

为遇到此问题的任何人创建了一个存储库.仅供参考:无法使用NSNetService达到目标,但能够使用net-mdns是 https://github.com/Raj123456788/ZeroConfiOS

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

这篇关于NSNetService使用C#分配IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 15:06