コード例 #1
0
        public unsafe void LeaveMulticastGroup(IPAddress groupAdress, uint interfaceIndex = 0)
        {
            var adress = groupAdress.GetAddressBytes();

            if (groupAdress.AddressFamily == AddressFamily.InterNetwork)
            {
                ip_mreq value = new ip_mreq();
                value.imr_interface.s_b4 = (byte)interfaceIndex;

                fixed(byte *a = adress)
                Unsafe.CopyBlock(value.imr_multiaddr.Address, a, (uint)adress.Length);

                if (SetSocketOption(IPPROTO_IP_SocketOptions.IP_DROP_MEMBERSHIP, (void *)&value, Marshal.SizeOf <ip_mreq>()) != 0)
                {
                    WinSock.ThrowLastWSAError();
                }
            }
            else if (groupAdress.AddressFamily == AddressFamily.InterNetworkV6)
            {
                ipv6_mreq value = new ipv6_mreq();
                value.ipv6mr_interface = interfaceIndex;

                fixed(byte *a = adress)
                Unsafe.CopyBlock(value.ipv6mr_multiaddr.Address, a, (uint)adress.Length);

                if (SetSocketOption(IPPROTO_IPV6_SocketOptions.IPV6_DROP_MEMBERSHIP, (void *)&value, Marshal.SizeOf <ipv6_mreq>()) != 0)
                {
                    WinSock.ThrowLastWSAError();
                }
            }
        }
コード例 #2
0
        //public unsafe void JoinMulticastGroup(IPAddress groupAdress, uint interfaceIndex = 0)
        //{
        //    group_req value = new group_req();
        //    value.gr_interface = interfaceIndex;
        //    var adress = groupAdress.GetAddressBytes();
        //    fixed (byte* a = adress)
        //        Unsafe.CopyBlock(value.gr_group.padding1, a, (uint)adress.Length);
        //    if (groupAdress.AddressFamily == AddressFamily.InterNetwork)
        //        value.gr_group.AdressFamily = ADDRESS_FAMILIES.AF_INET;
        //    else if (groupAdress.AddressFamily == AddressFamily.InterNetwork)
        //        value.gr_group.AdressFamily = ADDRESS_FAMILIES.AF_INET6;
        //    var res = SetSocketOption(MCAST_SocketOptions.MCAST_JOIN_GROUP, (void*)&value, Marshal.SizeOf<group_req>());
        //    WinSock.ThrowLastWSAError();
        //}
        public unsafe void JoinMulticastGroup(IPAddress groupAdress, uint interfaceIndex = 0)
        {
            var adress = groupAdress.GetAddressBytes();
            if (groupAdress.AddressFamily == AddressFamily.InterNetwork)
            {
                ip_mreq value = new ip_mreq();
                value.imr_interface.s_b4 = (byte)interfaceIndex;
                fixed (byte* a = adress)
                    Unsafe.CopyBlock(value.imr_multiaddr.Address, a, (uint)adress.Length);

                if (SetSocketOption(IPPROTO_IP_SocketOptions.IP_ADD_MEMBERSHIP, (void*)&value, Marshal.SizeOf<ip_mreq>()) != 0)
                    WinSock.ThrowLastWSAError();
            }
            else if (groupAdress.AddressFamily == AddressFamily.InterNetworkV6)
            {
                ipv6_mreq value = new ipv6_mreq();
                value.ipv6mr_interface = interfaceIndex;
                fixed (byte* a = adress)
                    Unsafe.CopyBlock(value.ipv6mr_multiaddr.Address, a, (uint)adress.Length);

                if (SetSocketOption(IPPROTO_IPV6_SocketOptions.IPV6_ADD_MEMBERSHIP, (void*)&value, Marshal.SizeOf<ipv6_mreq>()) != 0)
                    WinSock.ThrowLastWSAError();
            }
        }