Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

How to get IP address

michael_green
Beginner
1,918 Views
Hi All,

Does anyone know of a neat way to get the IP address of a machine from within a Fortran program? I can spawn a console program to run ipconfig, etc, but that's seems clumsy - I'm surprised there doesn't seem to be a Windows API function for this. Have I missed something?

Many thanks in advance,
Mike
0 Kudos
5 Replies
Steven_L_Intel1
Employee
1,918 Views
This MSDN article shows how to do it from VB using the Winsock API. You can probably figure out how to do this in Fortran from the example here.

I note that the gethostbyname function has been superseded by getaddrinfo from WS2_32.lib, but I also note that Intel Fortran doesn't provide an interface to that in module WS2. gethostbyname is in module WSOCK32.
0 Kudos
michael_green
Beginner
1,918 Views
Many thanks, Steve,

The MSDN article demonstrates how to get all the IP addresses used by the machine "if the machine is multi-homed" (to quote from the article). I have already retrieved that information from reading the registry, but none of it helps me because I need to know the IP address in use at the time the program is being run. If I can get that, I can use it to determine which of many servers to connect to. The logonserver environment variable looked good for a while, but it has proved totally unreliable and is a big disappointment.

Thanks again - and season's greetings from down under,
Mike
0 Kudos
IanH
Honored Contributor III
1,918 Views
Remember that machines don't have IP addresses - adapters do. There may be multiple IP addresses "in use" at any time, trivially 127.0.0.1 etc, but consider VPN's, multiple network cards, VPN's, IP4/IP6...

What are you trying to do?

If something isn't available in Winsock then there's a lower level IP helper library that gives you equivalent information to ipconfig. I've used it in the past from C to get MAC addresses for ethernet adapters.
0 Kudos
Paul_Curtis
Valued Contributor I
1,918 Views
See attached code module and library, which discovers the IP addresses of all active adapters, as well as the current network gateway IP and the MAC address of the computer.
0 Kudos
michael_green
Beginner
1,918 Views
Thanks Guys, that's exactly what I needed.

To answer the question "what am I trying to do?" - there are dozens of servers in my environment spread over an enormous geographical area. Software I develop needs to know where it is being run from in order to connect to the appropriate server. From a list I have been given by our systems people I can work out where the user is located by using the subnet IP address. This system works well when I capture the output from ipconfig and get the information from there, but I hate to see a console window flashing up each time anyone starts a program.

Many thanks again.

Mike
0 Kudos
Reply