× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



I've gone through the archives and it seems that the recommended way to get
local host IP address is via gethostbyname function call.

Not this again!

gethostbyname() looks up an IP address for an internet domain name. It searches your local "HOSTS TABLE" and it also searches DNS. Whether or not this will actually give you your "local host IP address" is entirely dependent on whether you have a domain name that can be looked up to find it in DNS.

It's not a smart way to approach the problem, and unless you're in control of how DNS is set up. you shouldn't expect it to work.

Furthermore, you don't assign an IP address to a host. You assign an IP address to a network interface. Sometimes, you even assign more than one IP address to a network interface.

Consequently, the "right way" to find out the IP address really depends on what you want to use it for.

Consider the scenario of an iSeries system (actually, this is true for all computers, not just the iSeries) who is connected to two different ethernet LANs, plus a dial-up to the internet. There are a minimum of 4 different IP addresses assigned to this iSeries:


127.0.0,1 -- this IP address is always present, and it's used when one
             program on the system wants to communicate with another one
             on the same system.


One is associated with the dial-up (Point to Point) interface for talking
to the internet, and will most likely change with each connection.

One is associated with each of the LAN interfaces.


So now you have 4 IP addresses?  which one is the "Right one"?

Well, if you're talking to another program on the same system (such as using FTP to move a file from one place to another, or to print a stream file from a batch job) it's safe to hardcode 127.0.0.1. The nice thing about this interface is that it's very fast, very secure (since it's only accessible from within the same system) and it'll always be there with the same address on every computer that your software gets installed on.

If you're trying to "talk-back" to a 5250 client that has connected, you'll need to find out which network interface they've come in through. The QDCRDEVD API is able to tell you the IP address of the "local" network interface that they used.

If you're writing a program that communicates over a TCP/IP network, the getsockname() API can be used to find out the IP address of the interface that the system routed your request through.

If you're simplying trying to look this up for the purposes of printing it on a report or keeping track of it for auditing purposes or something like that, you'll want to keep track of ALL of the IP addresses on the system.

I wrote a demonstration program that lists all of the IP addresses on the system (though, you could modify it to only list a particular network interface if you wanted to) that you can download from the following link:

http://www.iseriesnetwork.com/noderesources/code/clubtechcode/DspIpIfc.zip



As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.