• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# Sockets.MulticastOption类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中System.Net.Sockets.MulticastOption的典型用法代码示例。如果您正苦于以下问题:C# MulticastOption类的具体用法?C# MulticastOption怎么用?C# MulticastOption使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



MulticastOption类属于System.Net.Sockets命名空间,在下文中一共展示了MulticastOption类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: StartMulticastConnector

        static void StartMulticastConnector()
        {
            IPAddress localIPAddr = IPAddress.Any;
            IPEndPoint mcastEP = new IPEndPoint(mcastAddress, mcastPort);
            AsyncDatagramConnector connector = new AsyncDatagramConnector();

            connector.FilterChain.AddLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Encoding.UTF8)));

            // Set the local IP address used by the listener and the sender to 
            // exchange multicast messages. 
            connector.DefaultLocalEndPoint = new IPEndPoint(localIPAddr, 0);

            // Define a MulticastOption object specifying the multicast group  
            // address and the local IP address. 
            // The multicast group address is the same as the address used by the listener.
            MulticastOption mcastOption = new MulticastOption(mcastAddress, localIPAddr);
            connector.SessionConfig.MulticastOption = mcastOption;

            // Call Connect() to force binding to the local IP address,
            // and get the associated multicast session.
            IoSession session = connector.Connect(mcastEP).Await().Session;

            // Send multicast packets to the multicast endpoint.
            session.Write("hello 1", mcastEP);
            session.Write("hello 2", mcastEP);
            session.Write("hello 3", mcastEP);
        }
开发者ID:zhangf911,项目名称:Mina.NET,代码行数:27,代码来源:Program.cs


示例2: StartMulticastAcceptor

        static void StartMulticastAcceptor()
        {
            IPAddress localIPAddr = IPAddress.Any;
            AsyncDatagramAcceptor acceptor = new AsyncDatagramAcceptor();

            acceptor.FilterChain.AddLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Encoding.UTF8)));

            // Define a MulticastOption object specifying the multicast group  
            // address and the local IPAddress. 
            // The multicast group address is the same as the address used by the client.
            MulticastOption mcastOption = new MulticastOption(mcastAddress, localIPAddr);
            acceptor.SessionConfig.MulticastOption = mcastOption;

            acceptor.SessionOpened += (s, e) =>
            {
                Console.WriteLine("Opened: {0}", e.Session.RemoteEndPoint);
            };
            acceptor.MessageReceived += (s, e) =>
            {
                Console.WriteLine("Received from {0}: {1}", e.Session.RemoteEndPoint, e.Message);
            };

            acceptor.Bind(new IPEndPoint(localIPAddr, mcastPort));

            Console.WriteLine("Acceptor: current multicast group is: " + mcastOption.Group);
            Console.WriteLine("Acceptor: current multicast local address is: " + mcastOption.LocalAddress);
            Console.WriteLine("Waiting for multicast packets.......");
        }
开发者ID:zhangf911,项目名称:Mina.NET,代码行数:28,代码来源:Program.cs


示例3: MulticastMessageChannel

        /// <summary> 
        /// Constructor for UDP channels.
        /// </summary>
        /// <param name="localInfo"> The local endpoint to which you bind the UDP connection.
        /// <param name="remoteInfo"> A struct with the host name  and the port number 
        /// of the remote host
        /// </param>
        /// <exception cref="System.IO.IOException">  if an error occurs
        /// </exception>
        public MulticastMessageChannel(ConnectionInfo multicastInfo)
        {
            try
            {
                // Create the Socket
                udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                // Set the reuse address option
                udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);

                // Create an IPEndPoint and bind to it
                IPEndPoint bindaddrs = new IPEndPoint(IPAddress.Any, multicastInfo.Port);
                udpSocket.Bind(bindaddrs);

                // Define a MulticastOption object specifying the multicast group
                // address and the local IPAddress.
                // The multicast group address is the same as the address used by the server.
                System.Net.IPAddress ipAddress = IPAddress.Parse(multicastInfo.Addr);
                MulticastOption mcastOption = new MulticastOption(ipAddress, IPAddress.Any);

                // IP multicast loopback.
                udpSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastLoopback, 1);

                // Add membership in the multicast group
                //BIG NOTE: If you have an exception at this point and your computer is disconnected
                // using Windows, the problem is due to a lack of a loopback interface.
                // You need to install a loopback interface Adapter.
                // The Microsoft Loopback adapter is a tool for testing in a
                // virtual network environment where access to a network is not feasible.
                // Click Start, point to Settings, click Control Panel, and then double-click Add/Remove Hardware.
                // You will find it in the Manufacturers section, Microsoft.
                udpSocket.SetSocketOption(SocketOptionLevel.IP,
                                        SocketOptionName.AddMembership,
                                        new MulticastOption(ipAddress, IPAddress.Any));

                // Set the Time to Live
                udpSocket.SetSocketOption(SocketOptionLevel.IP,
                                            SocketOptionName.MulticastTimeToLive, 2);

                udpMulticastAddress = new IPEndPoint(ipAddress, multicastInfo.Port);
                udpSocket.Connect(udpMulticastAddress);
            }
            catch (SocketException e)
            {
                if (log.IsErrorEnabled)
                    log.Error("SocketException caught!. Message : " + e.Message);
                throw e;
            }
            catch (Exception e)
            {
                if (log.IsErrorEnabled)
                    log.Error("Exception caught!. Message : " + e.Message);
                throw e;
            }

            if (log.IsDebugEnabled)
                log.Debug("Connected Multicast, own channel local point: " + udpSocket.LocalEndPoint
                        + " Multicast Address " + udpSocket.RemoteEndPoint);
        }
开发者ID:superliujian,项目名称:Sxta,代码行数:68,代码来源:MulticastMessageChannel.cs


示例4: SendAsync

        public Task SendAsync(string data)
        {
            return Task.Factory.StartNew(() =>
            {
                IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());

                foreach(var ipAddress in localIPs.Where(a => a.AddressFamily == AddressFamily.InterNetwork))
                {
                    var mcastSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    mcastSocket.Bind(new IPEndPoint(ipAddress, 0));
                    var mcastOption = new MulticastOption(MulticastAdress);
                    mcastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, mcastOption);
                    var endPoint = new IPEndPoint(MulticastAdress, Port);
                    mcastSocket.SendTo(Encoding.ASCII.GetBytes(data), endPoint);
                }
            });
        }
开发者ID:CKrowiorsch,项目名称:WebApiDiscovery.Net,代码行数:17,代码来源:MulticastClient.cs


示例5: Constructor1

		[Test] // .ctor (IPAddress)
		public void Constructor1 ()
		{
			MulticastOption option;
			IPAddress group;

			group = IPAddress.Parse ("239.255.255.250");
			option = new MulticastOption (group);
			Assert.AreSame (group, option.Group, "#A:Group");
			Assert.AreEqual (0, option.InterfaceIndex, "#A:InterfaceIndex");
			Assert.AreEqual (IPAddress.Any, option.LocalAddress, "#A:LocalAddress");

			group = IPAddress.Parse ("ff02::1");
			option = new MulticastOption (group);
			Assert.AreSame (group, option.Group, "#B:Group");
			Assert.AreEqual (0, option.InterfaceIndex, "#B:InterfaceIndex");
			Assert.AreEqual (IPAddress.Any, option.LocalAddress, "#B:LocalAddress");
		}
开发者ID:Profit0004,项目名称:mono,代码行数:18,代码来源:MulticastOptionTest.cs


示例6: CreateListeningSocket

 protected virtual void CreateListeningSocket()
 {
     lock (Locker)
     {
         try
         {
             _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
             IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, _port);
             _socket.Bind(ipEndPoint);
             IPAddress ipAddress = IPAddress.Parse(_ipAddress);
             MulticastOption multicastOption = new MulticastOption(ipAddress);
             _socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 2);
             _socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, multicastOption);
             _endPointSender = new IPEndPoint(IPAddress.Any, 0);
         }
         catch (Exception e)
         {
             Trace.Error("Socket Creation Exception", e);
         }
     }
 }
开发者ID:KeiserCorp,项目名称:Keiser.MvxPlugins.Sockets,代码行数:21,代码来源:NetSocket.cs


示例7: ReceiveAsync

        public Task ReceiveAsync(Action<string> onData)
        {
            var cancellationToken = new CancellationToken();

            return Task.Factory.StartNew(() =>
            {
                var multicastSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                EndPoint localEndpoint = new IPEndPoint(IPAddress.Any, Port);

                multicastSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                multicastSocket.ExclusiveAddressUse = false;
                multicastSocket.Bind(localEndpoint);

                var multicastOption = new MulticastOption(MulticastAdress);

                multicastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, multicastOption);

                var bytes = new byte[1024];
                var remoteEndpoint = (EndPoint)new IPEndPoint(IPAddress.Any, 0);

                try
                {
                    while (!cancellationToken.IsCancellationRequested)
                    {
                        int length = multicastSocket.ReceiveFrom(bytes, ref remoteEndpoint);
                        onData(Encoding.ASCII.GetString(bytes, 0, length));
                    }

                    multicastSocket.Close();
                }

                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }, cancellationToken);
        }
开发者ID:CKrowiorsch,项目名称:WebApiDiscovery.Net,代码行数:37,代码来源:MulticastClient.cs


示例8: Constructor2

		[Test] // .ctor (IPAddress, IPAddress)
		public void Constructor2 ()
		{
			MulticastOption option;
			IPAddress group;
			IPAddress mcint;

			group = IPAddress.Parse ("239.255.255.250");
			mcint = IPAddress.Any;
			option = new MulticastOption (group, mcint);
			Assert.AreSame (group, option.Group, "#A:Group");
#if NET_2_0
			Assert.AreEqual (0, option.InterfaceIndex, "#A:InterfaceIndex");
#endif
			Assert.AreEqual (mcint, option.LocalAddress, "#A:LocalAddress");

			group = IPAddress.Parse ("ff02::1");
			mcint = IPAddress.IPv6Any;
			option = new MulticastOption (group, mcint);
			Assert.AreSame (group, option.Group, "#B:Group");
#if NET_2_0
			Assert.AreEqual (0, option.InterfaceIndex, "#B:InterfaceIndex");
#endif
			Assert.AreEqual (mcint, option.LocalAddress, "#B:LocalAddress");
		}
开发者ID:nlhepler,项目名称:mono,代码行数:25,代码来源:MulticastOptionTest.cs


示例9: GetMulticastOption

        public static unsafe SocketError GetMulticastOption(SafeCloseSocket handle, SocketOptionName optionName, out MulticastOption optionValue)
        {
            int optLevel, optName;
            GetPlatformOptionInfo(SocketOptionLevel.IP, optionName, out optLevel, out optName);

            var mreqn = new Interop.libc.ip_mreqn();
            var optLen = (uint)sizeof(Interop.libc.ip_mreqn);
            int err = Interop.libc.getsockopt(handle.FileDescriptor, optLevel, optName, &mreqn, &optLen);
            if (err == -1)
            {
                optionValue = default(MulticastOption);
                return GetLastSocketError();
            }

            var multicastAddress = new IPAddress((long)mreqn.imr_multiaddr.s_addr);
            var multicastInterface = new IPAddress((long)mreqn.imr_address.s_addr);
            optionValue = new MulticastOption(multicastAddress, multicastInterface);
            return SocketError.Success;
        }
开发者ID:nnyamhon,项目名称:corefx,代码行数:19,代码来源:SocketPal.Unix.cs


示例10: SetMulticastOption

        public static unsafe SocketError SetMulticastOption(SafeCloseSocket handle, SocketOptionName optionName, MulticastOption optionValue)
        {
            int optLevel, optName;
            GetPlatformOptionInfo(SocketOptionLevel.IP, optionName, out optLevel, out optName);

            var mreqn = new Interop.libc.ip_mreqn {
                imr_multiaddr = new Interop.libc.in_addr {
                    s_addr = unchecked((uint)optionValue.Group.GetAddress())
                }
            };
            if (optionValue.LocalAddress != null)
            {
                mreqn.imr_address.s_addr = unchecked((uint)optionValue.LocalAddress.GetAddress());
            }
            else
            {
                // TODO: what is the endianness of ipv6mr_ifindex?
                mreqn.imr_ifindex = optionValue.InterfaceIndex;
            }

            int err = Interop.libc.setsockopt(handle.FileDescriptor, optLevel, optName, &mreqn, (uint)sizeof(Interop.libc.ip_mreqn));
            return err == -1 ? GetLastSocketError() : SocketError.Success;
        }
开发者ID:nnyamhon,项目名称:corefx,代码行数:23,代码来源:SocketPal.Unix.cs


示例11: getMulticastOpt

        private MulticastOption getMulticastOpt(SocketOptionName optionName) {
            IPMulticastRequest ipmr = new IPMulticastRequest();
            int optlen = IPMulticastRequest.Size;

            // This can throw ObjectDisposedException.
            SocketError errorCode = UnsafeNclNativeMethods.OSSOCK.getsockopt(
                m_Handle,
                SocketOptionLevel.IP,
                optionName,
                out ipmr,
                ref optlen);

            GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::getMulticastOpt() UnsafeNclNativeMethods.OSSOCK.getsockopt returns errorCode:" + errorCode);

            //
            // if the native call fails we'll throw a SocketException
            //
            if (errorCode==SocketError.SocketError) {
                //
                // update our internal state after this socket error and throw
                //
                SocketException socketException = new SocketException();
                UpdateStatusAfterSocketError(socketException);
                if(s_LoggingEnabled)Logging.Exception(Logging.Sockets, this, "getMulticastOpt", socketException);
                throw socketException;
            }

#if BIGENDIAN
            ipmr.MulticastAddress = (int) (((uint) ipmr.MulticastAddress << 24) |
                                           (((uint) ipmr.MulticastAddress & 0x0000FF00) << 8) |
                                           (((uint) ipmr.MulticastAddress >> 8) & 0x0000FF00) |
                                           ((uint) ipmr.MulticastAddress >> 24));
            ipmr.InterfaceAddress = (int) (((uint) ipmr.InterfaceAddress << 24) |
                                           (((uint) ipmr.InterfaceAddress & 0x0000FF00) << 8) |
                                           (((uint) ipmr.InterfaceAddress >> 8) & 0x0000FF00) |
                                           ((uint) ipmr.InterfaceAddress >> 24));
#endif  // BIGENDIAN

            IPAddress multicastAddr = new IPAddress(ipmr.MulticastAddress);
            IPAddress multicastIntr = new IPAddress(ipmr.InterfaceAddress);

            MulticastOption multicastOption = new MulticastOption(multicastAddr, multicastIntr);

            return multicastOption;
        }
开发者ID:REALTOBIZ,项目名称:mono,代码行数:45,代码来源:Socket.cs


示例12: setMulticastOption

        private void setMulticastOption(SocketOptionName optionName, MulticastOption MR) {
            IPMulticastRequest ipmr = new IPMulticastRequest();

            ipmr.MulticastAddress = unchecked((int)MR.Group.m_Address);


            if(MR.LocalAddress != null){
                ipmr.InterfaceAddress = unchecked((int)MR.LocalAddress.m_Address);
            }
            else {  //this structure works w/ interfaces as well
                int ifIndex =IPAddress.HostToNetworkOrder(MR.InterfaceIndex);
                ipmr.InterfaceAddress   = unchecked((int)ifIndex);
            }

#if BIGENDIAN
            ipmr.MulticastAddress = (int) (((uint) ipmr.MulticastAddress << 24) |
                                           (((uint) ipmr.MulticastAddress & 0x0000FF00) << 8) |
                                           (((uint) ipmr.MulticastAddress >> 8) & 0x0000FF00) |
                                           ((uint) ipmr.MulticastAddress >> 24));

            if(MR.LocalAddress != null){
                ipmr.InterfaceAddress = (int) (((uint) ipmr.InterfaceAddress << 24) |
                                           (((uint) ipmr.InterfaceAddress & 0x0000FF00) << 8) |
                                           (((uint) ipmr.InterfaceAddress >> 8) & 0x0000FF00) |
                                           ((uint) ipmr.InterfaceAddress >> 24));
            }
#endif  // BIGENDIAN

            GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::setMulticastOption(): optionName:" + optionName.ToString() + " MR:" + MR.ToString() + " ipmr:" + ipmr.ToString() + " IPMulticastRequest.Size:" + IPMulticastRequest.Size.ToString());

            // This can throw ObjectDisposedException.
            SocketError errorCode = UnsafeNclNativeMethods.OSSOCK.setsockopt(
                m_Handle,
                SocketOptionLevel.IP,
                optionName,
                ref ipmr,
                IPMulticastRequest.Size);

            GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::setMulticastOption() UnsafeNclNativeMethods.OSSOCK.setsockopt returns errorCode:" + errorCode);

            //
            // if the native call fails we'll throw a SocketException
            //
            if (errorCode==SocketError.SocketError) {
                //
                // update our internal state after this socket error and throw
                //
                SocketException socketException = new SocketException();
                UpdateStatusAfterSocketError(socketException);
                if(s_LoggingEnabled)Logging.Exception(Logging.Sockets, this, "setMulticastOption", socketException);
                throw socketException;
            }
        }
开发者ID:REALTOBIZ,项目名称:mono,代码行数:53,代码来源:Socket.cs


示例13: Constructor3

		[Test] // .ctor (IPAddress, Int32)
		public void Constructor3 ()
		{
			MulticastOption option;
			IPAddress group;
			int interfaceIndex;

			group = IPAddress.Parse ("239.255.255.250");
			interfaceIndex = 0;
			option = new MulticastOption (group, interfaceIndex);
			Assert.AreSame (group, option.Group, "#A:Group");
			Assert.AreEqual (interfaceIndex, option.InterfaceIndex, "#A:InterfaceIndex");
			Assert.AreEqual (null, option.LocalAddress, "#A:LocalAddress");

			group = IPAddress.Parse ("ff02::1");
			interfaceIndex = 0;
			option = new MulticastOption (group, interfaceIndex);
			Assert.AreSame (group, option.Group, "#B:Group");
			Assert.AreEqual (interfaceIndex, option.InterfaceIndex, "#B:InterfaceIndex");
			Assert.AreEqual (null, option.LocalAddress, "#B:LocalAddress");

			group = IPAddress.Parse ("239.255.255.250");
			interfaceIndex = 124;
			option = new MulticastOption (group, interfaceIndex);
			Assert.AreSame (group, option.Group, "#C:Group");
			Assert.AreEqual (interfaceIndex, option.InterfaceIndex, "#C:InterfaceIndex");
			Assert.AreEqual (null, option.LocalAddress, "#C:LocalAddress");

			group = IPAddress.Parse ("ff02::1");
			interfaceIndex = 124;
			option = new MulticastOption (group, interfaceIndex);
			Assert.AreSame (group, option.Group, "#D:Group");
			Assert.AreEqual (interfaceIndex, option.InterfaceIndex, "#D:InterfaceIndex");
			Assert.AreEqual (null, option.LocalAddress, "#D:LocalAddress");

			group = IPAddress.Parse ("239.255.255.250");
			interfaceIndex = 0xFFFFFF;
			option = new MulticastOption (group, interfaceIndex);
			Assert.AreSame (group, option.Group, "#E:Group");
			Assert.AreEqual (interfaceIndex, option.InterfaceIndex, "#E:InterfaceIndex");
			Assert.AreEqual (null, option.LocalAddress, "#E:LocalAddress");

			group = IPAddress.Parse ("ff02::1");
			interfaceIndex = 0xFFFFFF;
			option = new MulticastOption (group, interfaceIndex);
			Assert.AreSame (group, option.Group, "#F:Group");
			Assert.AreEqual (interfaceIndex, option.InterfaceIndex, "#F:InterfaceIndex");
			Assert.AreEqual (null, option.LocalAddress, "#F:LocalAddress");
		}
开发者ID:Profit0004,项目名称:mono,代码行数:49,代码来源:MulticastOptionTest.cs


示例14: NetworkRequestAsync

        async Task NetworkRequestAsync(byte[] requestBytes,
                                              TimeSpan scanTime,
                                              int retries,
                                              int retryDelayMilliseconds,
                                              Action<string, byte[]> onResponse,
                                              System.Net.NetworkInformation.NetworkInterface adapter,
                                              CancellationToken cancellationToken)
        {
            // http://stackoverflow.com/questions/2192548/specifying-what-network-interface-an-udp-multicast-should-go-to-in-net

#if !XAMARIN
            if (!adapter.GetIPProperties().MulticastAddresses.Any())
                return; // most of VPN adapters will be skipped
#endif
            if (!adapter.SupportsMulticast)
                return; // multicast is meaningless for this type of connection

            if (OperationalStatus.Up != adapter.OperationalStatus)
                return; // this adapter is off or not connected

            if (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback)
                return; // strip out loopback addresses

            var p = adapter.GetIPProperties().GetIPv4Properties();
            if (null == p)
                return; // IPv4 is not configured on this adapter

            var ipv4Address = adapter.GetIPProperties().UnicastAddresses
                                    .FirstOrDefault(ua => ua.Address.AddressFamily == AddressFamily.InterNetwork)?.Address;

            if (ipv4Address == null)
                return; // could not find an IPv4 address for this adapter

            var ifaceIndex = p.Index;

            Debug.WriteLine($"Scanning on iface {adapter.Name}, idx {ifaceIndex}, IP: {ipv4Address}");

            using (var client = new UdpClient())
            {
                for (var i = 0; i < retries; i++)
                {
#if ANDROID
                    var mlock = wifi.CreateMulticastLock("Zeroconf lock");
#endif
                    try
                    {
#if ANDROID
                        mlock.Acquire();
#endif
                        client.Client.SetSocketOption(SocketOptionLevel.IP,
                                                      SocketOptionName.MulticastInterface,
                                                      IPAddress.HostToNetworkOrder(ifaceIndex));



                        client.ExclusiveAddressUse = false;
                        client.Client.SetSocketOption(SocketOptionLevel.Socket,
                                                      SocketOptionName.ReuseAddress,
                                                      true);
                        client.Client.SetSocketOption(SocketOptionLevel.Socket,
                                                      SocketOptionName.ReceiveTimeout,
                                                      scanTime.Milliseconds);
                        client.ExclusiveAddressUse = false;

                        
                        var localEp = new IPEndPoint(IPAddress.Any, 5353);

                        Debug.WriteLine($"Attempting to bind to {localEp} on adapter {adapter.Name}");
                        client.Client.Bind(localEp);
                        Debug.WriteLine($"Bound to {localEp}");

                        var multicastAddress = IPAddress.Parse("224.0.0.251");
                        var multOpt = new MulticastOption(multicastAddress, ifaceIndex);
                        client.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, multOpt);


                        Debug.WriteLine("Bound to multicast address");

                        // Start a receive loop
                        var shouldCancel = false;
                        var recTask = Task.Run(async
                                               () =>
                                               {
                                                   try
                                                   {
                                                       while (!shouldCancel)
                                                       {
                                                           var res = await client.ReceiveAsync()
                                                                                 .ConfigureAwait(false);
                                                           onResponse(res.RemoteEndPoint.Address.ToString(), res.Buffer);
                                                       }
                                                   }
                                                   catch (ObjectDisposedException)
                                                   {
                                                   }
                                               }, cancellationToken);

                        var broadcastEp = new IPEndPoint(IPAddress.Parse("224.0.0.251"), 5353);
                        Debug.WriteLine($"About to send on iface {adapter.Name}");
                        await client.SendAsync(requestBytes, requestBytes.Length, broadcastEp)
//.........这里部分代码省略.........
开发者ID:Lakritzator,项目名称:Zeroconf,代码行数:101,代码来源:NetworkInterface.cs


示例15: ReleaseSocket


//.........这里部分代码省略.........

        //            if (externalInterface == null)
        //            {
        //                // Pri3: Do something more helpful here
        //                throw new Exception(Strings.UnableToFindLocalRoutingInterface);
        //            }

        //            if (Utility.IsMulticast(endPoint.Address))
        //            {
        //                // Allow multiple binds to this socket, as it will still function properly
        //                //  (this is only the case if it is a multicast socket.  Unicast sockets fail to
        //                //   receive all data on all sockets)
        //                sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, -1);

        //                // We don't join the multicast group here, because we may not want to listen
        //                // to our own data (halfing our throughput).  jasonv - 10/28/2004
        //            }

        //            // Add the socket to the hashtable
        //            SockInterfacePair sip = new SockInterfacePair(sock, externalInterface);
        //            socks.Add(endPoint, sip);

        //            // Increase the socket's reference count
        //            ++sock.refCount;

        //            return sip;
        //        }
        //    }
        //}

        //internal static void ReleaseSharedSocket(IPEndPoint endPoint, UdpSocket sock)
        //{
        //    object sockObj = socks[endPoint];
        //    if (sockObj == null)
        //        throw new InvalidOperationException(Strings.SockDoesNotExistAsASharedSocket);

        //    lock (socks)
        //    {
        //        if (--sock.refCount <= 0)
        //        {
        //            // Leave the multicast group
        //            if (Utility.IsMulticast(endPoint.Address))
        //            {
        //                try
        //                {
        //                    if (endPoint.AddressFamily == AddressFamily.InterNetworkV6)
        //                    {
        //                        IPv6MulticastOption mo = new IPv6MulticastOption(endPoint.Address);
        //                        sock.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.DropMembership, mo);
        //                    }
        //                    else
        //                    {
        //                        MulticastOption mo = new MulticastOption(endPoint.Address);
        //                        sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, mo);
        //                    }
        //                }
        //                catch { } // The user of the socket *may* not have joined the multicast group (?)
        //            }

        //            // Remove ourselves from the shared pool
        //            socks.Remove(endPoint);

        //            // Close the socket
        //            try
        //            {
        //                sock.Close();
        //            }
        //            catch (ObjectDisposedException) { }
        //        }
        //    }
        //}

        internal static void ReleaseSocket(IPEndPoint endPoint, UdpSocket sock)
        {
            // Leave the multicast group
            if (Utility.IsMulticast(endPoint.Address))
            {
                try
                {
                    if (endPoint.AddressFamily == AddressFamily.InterNetworkV6)
                    {
                        IPv6MulticastOption mo = new IPv6MulticastOption(endPoint.Address);
                        sock.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.DropMembership, mo);
                    }
                    else
                    {
                        MulticastOption mo = new MulticastOption(endPoint.Address);
                        sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, mo);
                    }
                }
                catch { } // The user of the socket *may* not have joined the multicast group (?)
            }

            // Close the socket
            try
            {
                sock.Close();
            }
            catch (ObjectDisposedException) { }
        }
开发者ID:Wiladams,项目名称:NewTOAPIA,代码行数:101,代码来源:UdpSocket.cs


示例16: SetSocketOption

		[Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
		public void SetSocketOption3_DropMembershipIPv4_MulticastOption ()
		{
			IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");

			using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
				MulticastOption option = new MulticastOption (mcast_addr);

				s.Bind (new IPEndPoint (IPAddress.Any, 1901));
				s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
					option);
				s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
					option);
			}
		}
开发者ID:Therzok,项目名称:mono,代码行数:15,代码来源:SocketTest.cs


示例17: LocalAddress

		public void LocalAddress ()
		{
			IPAddress group;
			IPAddress local;
			MulticastOption option;

			local = Dns.GetHostEntry (string.Empty).AddressList [0];
			group = IPAddress.Parse ("239.255.255.250");
			option = new MulticastOption (group, local);
			local = IPAddress.Loopback;
			option.LocalAddress = local;
			Assert.AreSame (group, option.Group, "#A1");
			Assert.AreEqual (0, option.InterfaceIndex, "#A2");
			Assert.AreSame (local, option.LocalAddress, "#A3");
			local = Dns.GetHostEntry (string.Empty).AddressList [0];
			option.LocalAddress = local;
			Assert.AreSame (group, option.Group, "#B1");
			Assert.AreEqual (0, option.InterfaceIndex, "#B2");
			Assert.AreSame (local, option.LocalAddress, "#B3");
			option.LocalAddress = null;
			Assert.AreSame (group, option.Group, "#C1");
			Assert.AreEqual (0, option.InterfaceIndex, "#C2");
			Assert.IsNull (option.LocalAddress, "#C3");
			option = new MulticastOption (group, 5);
			local = IPAddress.Loopback;
			option.LocalAddress = local;
			Assert.AreSame (group, option.Group, "#D1");
			Assert.AreEqual (0, option.InterfaceIndex, "#D2");
			Assert.AreSame (local, option.LocalAddress, "#D3");
		}
开发者ID:Profit0004,项目名称:mono,代码行数:30,代码来源:MulticastOptionTest.cs


示例18: InterfaceIndex_Value_OutOfRange

		public void InterfaceIndex_Value_OutOfRange ()
		{
			IPAddress group = IPAddress.Parse ("239.255.255.250");
			MulticastOption option = new MulticastOption (group, 10);

			try {
				option.InterfaceIndex = -1;
				Assert.Fail ("#A1");
			} catch (ArgumentOutOfRangeException ex) {
				// Specified argument was out of the range of valid values
				Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
				Assert.IsNull (ex.InnerException, "#A3");
				Assert.IsNotNull (ex.Message, "#A4");
				Assert.AreEqual ("value", ex.ParamName, "#A5");
			}

			try {
				option.InterfaceIndex = 0x1000000;
				Assert.Fail ("#B1");
			} catch (ArgumentOutOfRangeException ex) {
				// Specified argument was out of the range of valid values
				Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
				Assert.IsNull (ex.InnerException, "#B3");
				Assert.IsNotNull (ex.Message, "#B4");
				Assert.AreEqual ("value", ex.ParamName, "#B5");
			}

			// ensure original value was retained
			Assert.AreEqual (10, option.InterfaceIndex, "#C");
		}
开发者ID:Profit0004,项目名称:mono,代码行数:30,代码来源:MulticastOptionTest.cs


示例19: InterfaceIndex

		public void InterfaceIndex ()
		{
			IPAddress group;
			IPAddress local;
			MulticastOption option;
		
			group = IPAddress.Parse ("239.255.255.250");
			option = new MulticastOption (group, 10);
			option.InterfaceIndex = 0;
			Assert.AreSame (group, option.Group, "#A1");
			Assert.AreEqual (0, option.InterfaceIndex, "#A2");
			Assert.IsNull (option.LocalAddress, "#A3");
			option.InterfaceIndex = 124;
			Assert.AreSame (group, option.Group, "#B1");
			Assert.AreEqual (124, option.InterfaceIndex, "#B2");
			Assert.IsNull (option.LocalAddress, "#B3");
			option.InterfaceIndex = 0xFFFFFF;
			Assert.AreSame (group, option.Group, "#C1");
			Assert.AreEqual (0xFFFFFF, option.InterfaceIndex, "#C2");
			Assert.IsNull (option.LocalAddress, "#C3");

			local = Dns.GetHostEntry (string.Empty).AddressList [0];
			option = new MulticastOption (group, local);
			option.InterfaceIndex = 10;
			Assert.AreSame (group, option.Group, "#D1");
			Assert.AreEqual (10, option.InterfaceIndex, "#D2");
			Assert.IsNull (option.LocalAddress, "#D3");
		}
开发者ID:Profit0004,项目名称:mono,代码行数:28,代码来源:MulticastOptionTest.cs


示例20: Group

		public void Group ()
		{
			IPAddress group;
			IPAddress local;
			MulticastOption option;

			local = Dns.GetHostEntry (string.Empty).AddressList [0];
			group = IPAddress.Parse ("239.255.255.250");
			option = new MulticastOption (group, local);
			group = IPAddress.Parse ("224.0.0.23");
			option.Group = group;
			Assert.AreSame (group, option.Group, "#A1");
			Assert.AreSame (local, option.LocalAddress, "#A2");
			group = IPAddress.Parse ("239.255.255.250");
			option.Group = group;
			Assert.AreSame (group, option.Group, "#B1");
			Assert.AreSame (local, option.LocalAddress, "#B2");
			group = IPAddress.Parse ("ff02::1");
			option.Group = group;
			Assert.AreSame (group, option.Group, "#C1");
			Assert.AreSame (local, option.LocalAddress, "#C2");
			option.Group = null;
			Assert.IsNull (option.Group, "#D1");
			Assert.AreSame (local, option.LocalAddress, "#D2");
			option = new MulticastOption (group, 5);
			group = IPAddress.Parse ("224.0.0.23");
			option.Group = group;
			Assert.AreSame (group, option.Group, "#E1");
			Assert.AreEqual (5, option.InterfaceIndex, "#E2");
			Assert.IsNull (option.LocalAddress, "#E3");
		}
开发者ID:Profit0004,项目名称:mono,代码行数:31,代码来源:MulticastOptionTest.cs



注:本文中的System.Net.Sockets.MulticastOption类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# Sockets.NetworkStream类代码示例发布时间:2022-05-26
下一篇:
C# Sockets.LingerOption类代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap