🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.internet.socket
6 messages
6 total messages Started by JohnG Tue, 15 Apr 2008 14:32
(INDY) UDP binding and broadcast
#325
Author: JohnG
Date: Tue, 15 Apr 2008 14:32
138 lines
4471 bytes
Hi,

I am having issues with UDP binding and UDP broadcasts. It 
appears that the binding is not enforced when broadcasting 
messages. When I bind to an IP\NIc in a multi nic system
the message is sent across both connections and received by 
connected computers.

I have include the system setup and sample app I am using to 
verify the results.

Thanks,

JohnG








System setup is as follows, there are no switches or hubs:

System 1
--------
Nic #1 10.90.80.41 / 255.255.255.0 (connects to system 2)
Nic #2 unplugged
Nic #3 10.90.90.6  / 255.255.255.0 (connects to system 3)

System 2
--------
Nic #1 10.90.80.42 / 255.255.255.0   connects to system 1
Nic #2 unplugged
Nic #3 unplugged



System 3
--------
Nic #1 10.90.90.5 /255.255.255.0 connects to system 1


Sample code is :
//
//header file
//
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <IdBaseComponent.hpp>
#include <IdComponent.hpp>
#include <IdUDPBase.hpp>
#include <IdUDPServer.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
	TEdit *EditBindIP;
	TButton *ButtonBindIP;
	TButton *ButtonSendMessage;
	TEdit *EditMessage;
	TIdUDPServer *IdUDPServer;
	TMemo *Memo1;
	void __fastcall ButtonSendMessageClick(TObject *Sender);
	void __fastcall ButtonBindIPClick(TObject *Sender);
	void __fastcall IdUDPServerUDPRead(TObject *Sender, TStream *AData,
          TIdSocketHandle *ABinding);
private:	// User declarations
public:		// User declarations
	__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

//
//source file
//

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ButtonSendMessageClick(TObject *Sender)
{
   if(!IdUDPServer->Active)
      ShowMessage("UDP not enabled.");

   IdUDPServer->Broadcast(EditMessage->Text,40);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ButtonBindIPClick(TObject *Sender)
{
	IdUDPServer->Active       = false;
	IdUDPServer->Bindings->Clear();

	TIdSocketHandle *pBinding = IdUDPServer->Bindings->Add();
	pBinding->IP              = EditBindIP->Text;
	pBinding->Port            = 40;
	IdUDPServer->Active       = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::IdUDPServerUDPRead(TObject *Sender, TStream *AData,
	  TIdSocketHandle *ABinding)
{
   AnsiString Text;

   if(AData->Size > 0)
   {
	  int iSize = AData->Size;
	  Text.SetLength(iSize);
	  AData->Read(Text.c_str(),iSize);

	  Memo1->Lines->Add("UDP received " + Text + " from " + ABinding->PeerIP);
   }
}
//---------------------------------------------------------------------------
Re: (INDY) UDP binding and broadcast
#327
Author: Remy Lebeau (TeamB)
Date: Tue, 15 Apr 2008 16:48
26 lines
1869 bytes
"JohnG" <whatis@email.com> wrote in message 
news:4804bc99@newsgroups.borland.com...

> I am having issues with UDP binding and UDP broadcasts.
> It appears that the binding is not enforced when broadcasting
> messages. When I bind to an IP\NIc in a multi nic system
> the message is sent across both connections and received
> by connected computers.

The Broadcast() method broadcasts to "255.255.255.255" by default, which 
sends to all available subnets.  If you are using Indy 10, then Broadcast() 
has an optional IP parameter so you can narrow down the broadcast to a 
specific subnet (Indy 9 does not have that option), ie:

    void __fastcall TForm1::ButtonSendMessageClick(TObject *Sender)
    {
        if( !IdUDPServer->Active )
            ShowMessage("UDP not enabled.");

        // 10.90.80.255 = broadcast address of the "10.90.80.xxx" subnet
        IdUDPServer->Broadcast(EditMessage->Text, 40, "10.90.80.255");
    }


Gambit 
Re: (INDY) UDP binding and broadcast
#329
Author: johnG
Date: Tue, 15 Apr 2008 17:52
11 lines
1300 bytes
>The Broadcast() method broadcasts to "255.255.255.255" by >default, which sends to all available subnets.

Does it broadcast to all subnets accessible by a specific (physical network) network adapter, or all subnets across all adapters? Meaning that binding does not apply when calling broadcast()?


> If you are using Indy 10,
 Sorry, using indy 9.0.50,but is upgrading the only way to prevent sending messages across all network adapters?


JohnG
Re: (INDY) UDP binding and broadcast
#332
Author: Remy Lebeau (TeamB)
Date: Tue, 15 Apr 2008 19:08
35 lines
2556 bytes
"johnG" <whatis@email.com> wrote in message 
news:4804eb76$1@newsgroups.borland.com...

> Does it broadcast to all subnets accessible by a specific (physical
> network) network adapter, or all subnets across all adapters?

Using "255.255.255.255" as the destination IP will send the data on all 
physical adapters.  This is documented by Microsoft:

    sendto() function
    http://msdn2.microsoft.com/en-us/library/ms740148.aspx

    "To send a broadcast (on a SOCK_DGRAM only), the address pointed to by 
the to parameter can be constructed to contain the special IPv4 address 
INADDR_BROADCAST (defined in Winsock2.h), together with the intended port 
number. If the address pointed to by the to parameter contains the 
INADDR_BROADCAST address and intended port, then the broadcast will be sent 
out on all interfaces to that port.

    If the broadcast should be sent out only on a specific interface, then 
the address pointed to by the to parameter should contain the subnet 
broadcast address for the interface and the intended port. For example, an 
IPv4 network address of 192.168.1.0 with a subnet mask of 255.255.255.0 
would use a subnet broadcast address of 192.168.1.255."

INADDR_BROADCAST is defined as $FFFFFFFF, aka "255.255.255.255".

> Sorry, using indy 9.0.50,but is upgrading the only way to
> prevent sending messages across all network adapters?

Yes, unless you manually re-write Broadcast() and then re-compile Indy.


Gambit 
Re: (INDY) UDP binding and broadcast
#333
Author: Bob Gonder
Date: Tue, 15 Apr 2008 19:16
16 lines
1821 bytes
johnG wrote:

>>The Broadcast() method broadcasts to "255.255.255.255" by >default, which sends to all available subnets.
>
>Does it broadcast to all subnets accessible by a specific (physical network) network adapter, or all subnets across all adapters? Meaning that binding does not apply when calling broadcast()?

Broadcast is...broad...it goes to all nodes that meet the mask.
AFAIK it has no concept of network adapters.

Look again at Remy's last post..
        // 10.90.80.255 = broadcast address of the "10.90.80.xxx" subnet
If all of your adapters are in the 10.90.80.x range, they will all broadcast.
If one of your adapters is, say 10.90.81.x, it won't broadcast.

There is a discussion of this (it's .Net, but still applies) here:
http://msdn2.microsoft.com/en-us/library/tst0kwb1(vs.71).aspx
Re: (INDY) UDP binding and broadcast
#336
Author: johnG
Date: Tue, 15 Apr 2008 19:56
12 lines
1308 bytes
Thanks Remy Lebeau

>Yes, unless you manually re-write Broadcast() and then re->compile Indy.

Seems like a good reason to upgrade to Indy 10 :)

I am going to try upgrading to Indy 10 on a test system to determine how much code would need to be modified to support INDY 10.

Any advice on making the install\upgrade of Indy painless? 

JohnG
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads