Solved

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

Posted on 2013-06-16
9
1,419 Views
Last Modified: 2013-11-13
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.
0
Comment
Question by:DMTrump
  • 5
  • 4
9 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 39252353
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
 

Author Comment

by:DMTrump
ID: 39255151
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
 
LVL 24

Expert Comment

by:jimyX
ID: 39255446
>   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
 

Author Comment

by:DMTrump
ID: 39256794
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 24

Expert Comment

by:jimyX
ID: 39260548
>   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
 

Author Comment

by:DMTrump
ID: 39260573
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
 
LVL 24

Accepted Solution

by:
jimyX earned 500 total points
ID: 39260692
>   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
 

Author Comment

by:DMTrump
ID: 39263591
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
 

Author Closing Comment

by:DMTrump
ID: 39263594
Thanks a lot for sticking with me!
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

Please see preceding article here: http://www.experts-exchange.com/Networking/Operating_Systems/A_11209-Root-Bridge-Election.html Figure 1 After Root Bridge has been elected, then what?..... Let's start by defining a Root Port in la…
Before I go to far, let's explain HA (High Availability) and why you should consider it.  High availability is the mechanism used to provide redundancy to any service at the same site and appears as a single service to the users of that service.  As…
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now