Board index » cppbuilder » Binding IdUDPServer

Binding IdUDPServer


2007-11-27 10:50:26 PM
cppbuilder51
Hello,
I am trying to bind a TIdUDPServer to a specific IP.
System will always have multiple nics. I am using the following
code to bind the IP.
IdUDPServer1->Binding->IP = Edit1->Text;
try
{
IdUDPServer1->Binding->Bind();
}
catch(const Exception& error)
{
ShowMessage(error.Message);
}
I always gets a Socket error #10022 ? Is this an incorrect way to bind?
If I use the following code, I get no exception
IdUDPServer->Bindings->Add();
IdUDPServer->Bindings->Items[0]->Port = 24;
IdUDPServer->Bindings->Items[0]->IP = IP;
IdUDPServer->Active = True;
However I don't think the binding is working!
Any help would be appreciated.
Thanks,
JohnG
 
 

Re:Binding IdUDPServer

"JohnG" < XXXX@XXXXX.COM >wrote in message
Quote
I am trying to bind a TIdUDPServer to a specific IP.
You are going about it all wrong.
Quote
System will always have multiple nics.
You need to use the server's Bindings collection instead, where each entry
in the collection cooresponds to a particular NIC IP that you want to use.
Quote
I am using the following code to bind the IP.
DO NOT call Bind() directly. TIdUDPServer will handle that automatically
for you. All you have to do is set up the Bindings collection as desired
and then set the server's Active property to true. Nothing else.
Quote
Is this an incorrect way to bind?
Yes.
Quote
If I use the following code, I get no exception
That is what you are supposed to be using. However, you don't need to index
into the collection like that. Add() returns the new Binding object that
was added:
TIdSocketHandle *pBinding = IdUDPServer->Bindings->Add();
pBinding->IP = IP;
pBinding->Port = 24;
IdUDPServer->Active = true;
Quote
However I don't think the binding is working!
Yes, it will, as long as the IP/Port you are trying to use are actually
available for use.
Gambit
 

Re:Binding IdUDPServer

Quote
TIdSocketHandle *pBinding = IdUDPServer->Bindings->Add();
pBinding->IP = IP;
pBinding->Port = 24;
IdUDPServer->Active = true;
This was the approach I used at first but I did not get the
results I expected. When using IdUDPServer->Broadcast,
does it respect the binding?
In most case it worked as expected, however in the following situation it did not:
When two nics are connected to two different networks yet
their addresses are of the same Network segment i.e. nic#1 =
10.90.90.95 and nic#2 = 10.90.90.96 the broadcast fails to be
sent from the correct Nic.
If I change one of the nics to 10.90.80.X the message is
broadcast correctly.
Thanks,
JohnG
 

{smallsort}

Re:Binding IdUDPServer

"JohnG" < XXXX@XXXXX.COM >wrote in message
Quote
This was the approach I used at first but I did not get the
results I expected. When using IdUDPServer->Broadcast,
does it respect the binding?
The Binding property always returns the first item in the Bindings
collection. If you want to broadcast to all of the items in the Bindings
collection, then you have to broadcast to each one individually.
TIdUDPServer does not expose functionality to do that for you, so you have
to set it up manually, ie:
void SetBroadcastFlag(TIdSocketHandle *ABinding, BOOL AEnabled)
{
ABinding->SetSockOpt(Id_SOL_SOCKET, Id_SO_BROADCAST,
(char*)&AEnabled, sizeof(BOOL));
}
for(int i = 0; i < IdUDPServer1->Bindings->Count; ++i)
{
TIdSocketHandle *pBinding = IdUDPServer1->Bindings->Items[i];
SetBroadcastFlag(pBinding, TRUE);
pBinding->Send("255.255.255.255", ThePort, TheData);
SetBroadcastFlag(pBinding, IdUDPServer1->BroadcastEnabled);
}
Now, in your case, both Bindings entries are on the same network segement,
so it shouldn't matter which entry you broadcast from since they are going
to go to the same network.
Quote
When two nics are connected to two different networks yet
their addresses are of the same Network segment i.e. nic#1 =
10.90.90.95 and nic#2 = 10.90.90.96 the broadcast fails to
be sent from the correct Nic.
How are you checking that? Which nic does it actually go out of, and which
one did you want it to use?
Gambit
 

Re:Binding IdUDPServer

Quote
If you want to broadcast to all of the items in the Bindings
collection,
No. I have 2 Nics (or more). I always want to broadcast from a
specific nic. Lets says nic #1.
Quote
How are you checking that? Which nic does it actually go out>of, and which one did you want it to use?
I verify the IP on the app receiving the broadcast by outputing ABinding->PeerIP;
On application start up I execute the following code:
IdUDPServer->Bindings->Clear();
TIdSocketHandle *pBinding = IdUDPServer->Bindings->Add();
pBinding->IP = Nic1_IPADDRESS;
IdUDPServer->Active = true;
My understanding of the above code is that I will always be
binding to one ip address.
Quote
Now, in your case, both Bindings entries are on the same
network segement, so it shouldn't matter which entry you
broadcast from since they are going to go to the same>network.
When they are on the same network segment (physical network)
the message does get broadcast from the correct nic.
However when they are not on the same physical network but the ips are i.e. nic#1 = 10.90.90.95 and nic#2 = 10.90.90.96 the broadcast fails to be sent from the correct Nic.
Hopefully this will make it more clear
nic#1 (10.90.90.95) ->Network A
nic#2 (10.90.90.96) ->Network B
The message will not be broadcast.
nic#1 (10.90.90.95) ->Network A
nic#2 (10.90.90.96) ->Network A
The message will be broadcast from the correct nic.
If you wondering why someone would configure in this fashion.
This situation is possible if both nics fail to find a DHCP
server and are configured using AutoIP (169.x.x.x) and are
connected to 2 different networks.
JohnG
 

Re:Binding IdUDPServer

"JohnG" < XXXX@XXXXX.COM >wrote in message
Quote
No. I have 2 Nics (or more). I always want to broadcast
from a specific nic. Lets says nic #1.
Then you need to locate the particular Binding for that NIC, and then you
can broadcast to it as I showed you.
Quote
On application start up I execute the following code:
<snip>
My understanding of the above code is that I will always be
binding to one ip address.
That is correct.
Quote
However when they are not on the same physical network but the ips are
How can the IPs be in the same network segment if they are not on the same
physical network? Are you connecting to two completely independant networks
that happen to be using the exact same addressing scheme?
Quote
i.e. nic#1 = 10.90.90.95 and nic#2 = 10.90.90.96 the broadcast fails to be
sent from the correct Nic.
If you are broadcasting to a specific Binding, then the broadcast has to go
out over the network that Binding is physically connected to. It can't
cross to another network that it is not connected to.
Quote
Hopefully this will make it more clear
I suspect your IPs are confusing the OS's own internal routing of packets
since the two networks are using the same addressing scheme. Packets for
Network B are likely being routed to Network A and vice versa because the OS
doesn't know any better. That is just a guess, though. I'm not a
networking expert.
Quote
If you wondering why someone would configure in this fashion.
This situation is possible if both nics fail to find a DHCP
server and are configured using AutoIP (169.x.x.x) and are
connected to 2 different networks.
If the DHCP servers are not found, then I don't see how the PC can be
connected to either network since it can't be assigned appropriate IPs
correctly.
Gambit
 

Re:Binding IdUDPServer

If the DHCP servers are not found, then I don't see how the PC can be
Quote
connected to either network since it can't be assigned appropriate IPs
correctly.


I believe this describes my problem and the solution is
acceptable support.microsoft.com/kb/q244268
Thanks for your help
JohnG