代码如下:
public async void SimpleSend(bool bindClient, IByteBufferAllocator allocator, AddressFamily addressFamily, byte[] expectedData, int count)
{
var clientGroup = new MultithreadEventLoopGroup(1);
var clientBootstrap = new Bootstrap();
clientBootstrap
.Group(clientGroup)
.ChannelFactory(() => new SocketDatagramChannel(addressFamily))
.Option(ChannelOption.Allocator, allocator)
.Option(ChannelOption.SoBroadcast, true)
.Option(ChannelOption.IpMulticastLoopDisabled, false)
.Handler(new ActionChannelInitializer<IChannel>(channel =>
{
channel.Pipeline.AddLast("Test", new NetUtil.TestHandler());
}));
var clientEndPoint = new IPEndPoint(
addressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any,
IPEndPoint.MinPort);
IPAddress add = IPAddress.Parse(this.txtIP.Text);
clientBootstrap
.LocalAddress(clientEndPoint)
.RemoteAddress(new IPEndPoint(add.Address, Int16.Parse(this.txtPort.Text)));
Task <IChannel> task = null;
if (bindClient)
{
IPAddress addressLocal = NetUtil.GetLoopbackAddress(addressFamily);
task = clientBootstrap.BindAsync(clientEndPoint); //(clientEndPoint);
Console.WriteLine(task);
Int64 i = 1;
while (!task.IsCompleted)
{
Console.WriteLine(task.Status); //《《==此处“WaitingForActivation”
Thread.Sleep(10);
}
clientChannel = task.Result;
Console.WriteLine(task.AsyncState);
}
else
{
task = (Task<IChannel>)clientBootstrap.RegisterAsync();
clientChannel = task.Result;
}