Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UDP get exception when invoke bootstrap.BindAsync() #57

Closed
jasmine999 opened this issue Jun 3, 2021 · 1 comment
Closed

UDP get exception when invoke bootstrap.BindAsync() #57

jasmine999 opened this issue Jun 3, 2021 · 1 comment

Comments

@jasmine999
Copy link

var group = new MultithreadEventLoopGroup();

        try
        {
            var bootstrap = new Bootstrap();
            bootstrap
                .Group(group)
                .Channel<SocketDatagramChannel>()
                .Option(ChannelOption.SoBroadcast, true)
                .Handler(new ActionChannelInitializer<IChannel>(channel =>
                {
                    channel.Pipeline.AddLast(new LoggingHandler("CONN"));
                    channel.Pipeline.AddLast("Quote", new QuoteOfTheMomentClientHandler());
                }));

            
            IChannel clientChannel = await bootstrap.BindAsync();

when invoke await bootstrap.BindAsync() will get exception:localAddress must be set beforehand

is it we can not invoke bootstrap.BindAsync() method without any parameters? if so, how can we let the system to random choose the ipaddress and port?

@yyjdelete
Copy link
Collaborator

You can use port 0 to bind with an random available port, but there is not option for ***random ipaddress **. IPAddress.Any will to bind on all you eth and loopback, IPAddress.Loopback for loopback only, (IPv6) for ipv6 only(ipv4 will also bind on ipv6 if Socket.DualMode is not set to false), and any special one ipaddress of you eth for single eth.

I'm not sure, but for boardcast with UDP, maybe you must special one ipaddress of you eth, and you can get them by

NetworkInterface.GetAllNetworkInterfaces().Where(ni => ni.OperationalStatus == OperationalStatus.Up).SelectMany(ni => ni.GetIPProperties().UnicastAddresses).ToArray()

@cuteant cuteant closed this as completed Jun 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants