当前位置: 首页>>代码示例>>C#>>正文


C# EndPoint.BestAddressFamily方法代码示例

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


在下文中一共展示了EndPoint.BestAddressFamily方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TcpServerSocketChannel_can_accept_connection_on_any_valid_Endpoint

        public Property TcpServerSocketChannel_can_accept_connection_on_any_valid_Endpoint(EndPoint ep)
        {
            var ip = ep as IPEndPoint;
            var family = ep.BestAddressFamily();

            // TODO: remove this code once https://bugzilla.xamarin.com/show_bug.cgi?id=35536 is fixed

            if (IsMono && family == AddressFamily.InterNetworkV6 && ep is DnsEndPoint)
            {
                family = AddressFamily.InterNetwork;
            }

            IChannel s = null;
            IChannel c = null;
            try
            {
                var sb = new ServerBootstrap()
                    .ChannelFactory(() => new TcpServerSocketChannel(family))
                    .ChildHandler(new ActionChannelInitializer<TcpSocketChannel>(channel => { }))
                    .PreferredDnsResolutionFamily(family)
                    .Group(_serverGroup);

                s = sb.BindAsync(ep).Result;

                var cb = new ClientBootstrap()
                    .ChannelFactory(() => new TcpSocketChannel(family))
                    .Option(ChannelOption.TcpNodelay, true)
                    .Option(ChannelOption.ConnectTimeout, TimeSpan.FromMilliseconds(100))
                    .PreferredDnsResolutionFamily(family)
                    .Handler(new ActionChannelInitializer<TcpSocketChannel>(channel => { }))
                    .Group(_clientGroup);

                var clientEp = s.LocalAddress;
                if (ip != null) // handle special case of 0.0.0.0, which clients can't connect to directly.
                {
                    if (ip.Address.Equals(IPAddress.Any))
                        clientEp = new IPEndPoint(IPAddress.Loopback, ((IPEndPoint) s.LocalAddress).Port);
                    if (ip.Address.Equals(IPAddress.IPv6Any))
                        clientEp = new IPEndPoint(IPAddress.IPv6Loopback, ((IPEndPoint) s.LocalAddress).Port);
                }

                c = cb.ConnectAsync(clientEp).Result;
                c.WriteAndFlushAsync(Unpooled.Buffer(4).WriteInt(2)).Wait(20);

                return c.IsOpen.Label("Channel should be open")
                    .And(c.IsActive).Label("Channel should be active")
                    .And(c.IsWritable).Label("Channel should be writable");
            }
            finally
            {
                try
                {
                    c?.CloseAsync().Wait(TimeSpan.FromMilliseconds(200));
                    s?.CloseAsync().Wait(TimeSpan.FromMilliseconds(200));
                }
                catch
                {
                }
            }
        }
开发者ID:helios-io,项目名称:helios,代码行数:60,代码来源:DnsResolutionAndBindingSpec.cs

示例2: TcpSocketServerChannel_can_bind_on_any_valid_EndPoint

        public Property TcpSocketServerChannel_can_bind_on_any_valid_EndPoint(EndPoint ep)
        {
            IChannel c = null;
            var family = ep.BestAddressFamily();

            // TODO: remove this code once https://bugzilla.xamarin.com/show_bug.cgi?id=35536 is fixed
            if (IsMono && family == AddressFamily.InterNetworkV6 && ep is DnsEndPoint)
            {
                family = AddressFamily.InterNetwork;
            }

            try
            {
                var sb = new ServerBootstrap()
                    .ChannelFactory(() => new TcpServerSocketChannel(family))
                    .ChildHandler(new ActionChannelInitializer<TcpSocketChannel>(channel => { }))
                    .PreferredDnsResolutionFamily(family)
                    .Group(_serverGroup);

                c = sb.BindAsync(ep).Result;

                return c.IsOpen.Label("Channel should be open").And(c.IsActive).Label("Channel should be active");
            }
            finally
            {
                c?.CloseAsync().Wait(TimeSpan.FromMilliseconds(200));
            }
        }
开发者ID:helios-io,项目名称:helios,代码行数:28,代码来源:DnsResolutionAndBindingSpec.cs


注:本文中的System.Net.EndPoint.BestAddressFamily方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。