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.
DMTrumpAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
jimyXConnect With a Mentor Commented:
>   putting the proper address into a edit component obviously isn't enough.

And why is that?

You can test the stored IP against the list of IP's from 'GStack.LocalAddresses' as follows:
  TIdStack.IncUsage;
  try
    if GStack.LocalAddresses.IndexOf(Edit1.Text) < 0 then
      ShowMessage('Error...')
    else
      idComponent.Host or IP := Edit1.Text;
  finally
    TIdStack.DecUsage;
  end;

Open in new window


Here is a simple example using idUDP:
procedure TForm1.BtnUDPClientConnectClick(Sender: TObject);
begin
  IdUDPClient.Host := 'Your-Server-IP-Goes-Here';
  IdUDPClient.Port := 1200;
  IdUDPClient.Connect;
end;

procedure TForm1.BtnUDPServerConnectClick(Sender: TObject);
begin
  TIdStack.IncUsage;
  try
    if GStack.LocalAddresses.IndexOf(Edit1.Text) < 0 then
      ShowMessage('Error...')  // Notify the user of the wrong IP address
    else
      begin
        with IdUDPServer.Bindings.Add do
          begin
            IP := Edit1.Text;
            Port := 1200;
          end;
        IdUDPServer.Active := True;
      end;
  finally
    TIdStack.DecUsage;
  end;
end;

Open in new window

0
 
jimyXCommented:
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.
0
 
DMTrumpAuthor Commented:
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?
0
NEW Internet Security Report Now Available!

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out this quarters report on the threats that shook the industry in Q4 2017.

 
jimyXCommented:
>   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.
0
 
DMTrumpAuthor Commented:
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?
0
 
jimyXCommented:
>   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.
0
 
DMTrumpAuthor Commented:
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>
0
 
DMTrumpAuthor Commented:
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!
0
 
DMTrumpAuthor Commented:
Thanks a lot for sticking with me!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.