本文整理汇总了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
{
}
}
}
示例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));
}
}