问题
I have three network adapter, two effective and one 'VirtualBox Host-Only Network'.
When trying to send broadcast with the code bellow (binding the right IP), the broadcast data getting sent on the VirtualBox Network adapter (checked with wireshark) and of course getting no answer.
FIdUDPServer.Binding.IP := Settings.netInterfaces[settings.sett_net_interface].AddrIP;
FIdUDPServer.BroadcastEnabled := True;
FIdUDPServer.Broadcast(udpDiscovery, BCport);
Im trying to bind the IP of my real adapter to IdUDPServer (Binding.IP) yet the broadcast being sent from the wrong adapter.
When i disable the virtualbox adapter, again im getting answers. What is wrong here? IDE is Delphi 2010.
回答1:
Accessing the TIdUDPServer.Binding
property binds and activates the server if it is not already, and then returns the 1st binding object that the server is bound to. Changing the Binding.IP
property while the TIdUDPServer
is activated has no effect. You must populate the TIdUDPServer.Bindings
collection with the desired local binding(s) prior to activating the server, and then you can broadcast using the desired binding object(s) from the collection after the server is activated. For example:
FIdUDPServer.Active := False;
FIdUDPServer.Bindings.Clear.
FIdUDPServer.Bindings.Add.IP := Settings.netInterfaces[settings.sett_net_interface].AddrIP;
FIdUDPServer.BroadcastEnabled := True;
FIdUDPServer.Active := True;
FIdUDPServer.Broadcast(udpDiscovery, BCport);
// or:
// FIdUDPServer.Bindings[index].Broadcast(udpDiscovery, BCport);
来源:https://stackoverflow.com/questions/40783520/indy-tudpserver-broadcast