Link to home
Start Free TrialLog in
Avatar of DMTrump
DMTrumpFlag for United States of America

asked on

How to Assign a user selected IP to Indy 10.1.5 GStack.LocalAddress

A client has asked me to make it possible to choose other than the default (first) local IP address (when there is more than one NIC) In a custom application I've written for them using the Indy 10.1.5 UDP component..

I can get the list of available addresses thusly (where cmbxLocalAddresses is a Tcombobox)
     cmbxLocalAddresses.Items.Assign(GStack.LocalAddresses);
But GStack.LocalAddress is a read only property and I can't see a way of setting (assigning) the selected value to that GStack Property.

Does anyone have any suggestions.

Unfortunately, I don't have a machine with more than one NIC so I'm hoping for a proven solution - but if not that, at least some ideas.
Avatar of jimyX
jimyX

You do not have to do that. No need to change the GStack.LocalAddress. Because it's designed to get the first address. Instead you can return the index of the selected IP address from the GStack.LocalAddresses[index], as follows:

function GetlocalIPByIndex(idx:Integer): String;
begin
  TIdStack.IncUsage;
  try  // you may check here whether the idx is within the range of 0 and  LocalAddresses.count
    Result := GStack.LocalAddresses[idx];
  finally
    TIdStack.DecUsage;
  end;
end;

procedure TForm1.BtnAssigIPClick(Sender: TObject);
begin
   EdIPAddress.Text := GetlocalIPByIndex(CBxIPAddresses.ItemIndex);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  TIdStack.IncUsage;
  try
    CBxIPAddresses.Items.Assign(GStack.LocalAddresses);
    CBxIPAddresses.ItemIndex := 0;
  finally
    TIdStack.DecUsage;
  end;
end;

Open in new window

When the Button BtnAssigIP is clicked the selected IP Address is assigned to the Edit EdIPAddress.

If you want to try this without having many NIC's, you may install any virtual machine that creates Networks Adapters such as Oracle VM VirtualBox.
Avatar of DMTrump

ASKER

Your code is nearly identical to what I was already doing.  The problem is that the two nics are connected to two different local networks and another program (not mine) is likely using one of the nics and the custoemr wants to make sure that my appoication connects to the proper nic.

So my indy UDP app needs to be assigned to connect to a specific NIC whether or not the other application is already running.

It seems to me that if there is no way so select the address, then the system depends on the order in which the applications are launched - is that correct?

If that's so then I could solve their problem by providing an applet that when ran launched the two applications in the proper order.
Is that the best solution?
>   the system depends on the order in which the applications are launched

How is the starting order affects selecting the IP address?

You can not detect which IP/NIC to connect to by the order of starting.
Your application could be bind to a specific IP but can not be bind to NIC.

What I meant is, that you gotta give the option to the customer to select which IP to connect to. Then that IP would be the default IP unless it was changed by the customer.

Would you elaborate on what happens if the starting order changed.
Avatar of DMTrump

ASKER

It's obvious you have a level of understanding about NICs and IP address beyond what I have so let me re-phrase my question:

The customer has a computer with two Network cards.  One is connected to Network A, the other is connected to network B.  My application needs to communicate with other systems on network B.  There is another application running on that computer that needs to talk to network A.  I have no control over that other application.
How can I make sure that my application (written in Delphi 2007 using Indy 10.1.5) always connects to network B?
>   My application needs to communicate with other systems on network B.

Your application will not know if it was not told by someone.
It's going to be the customer to assign the IP address in your application to establish the communication.

So it's a process of having a mechanism to ask the customer, at the first use, to provide the IP address of network B (or wherever he/she wants your application to connect to), then store that IP for whenever your application needs to communicate with network B you just recall it. And then give an option within your application to change that IP address in case the Network Admin decided to expand/change the IP structure.
Avatar of DMTrump

ASKER

Okay, that's the logic - I understood that much - And I already have the code to make the selection from the list of addresses.  What I don't know is how to code that assignment in Indy/Delphi. Just putting the proper address into a edit component obviously isn't enough.  If you can show me code that does that actual assignment we have a winner!

Thanks for following along with me as I train myself! <BG>
ASKER CERTIFIED SOLUTION
Avatar of jimyX
jimyX

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DMTrump

ASKER

Thanks a bunch.  Maybe it should have been obvious - but, as the saying goes: "Not to me!"

I appreciate your help and have awarded you the full points and a top rating!
Avatar of DMTrump

ASKER

Thanks a lot for sticking with me!