Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving local IP?

Status
Not open for further replies.

dex1123

Programmer
May 9, 2000
35
BG
Hello!<br><br>I wrote this small function but it always returns &quot;127.0.0.1&quot;. I would like to be able to get my dynamically assigned IP when I connect to the net. What's wrong with this code?<br><br>char* GetLocalIP(void)&nbsp;&nbsp;<br>{<br> WORD wVerReq = MAKEWORD(1, 1);<br> WSADATA wsaData;<br> int error = 0;<br> HOSTENT* hostent;<br> in_addr* pPtr;<br> char* result;<br><br> error = WSAStartup(wVerReq, &wsaData);<br> if (error != 0)<br> return &quot;winsock.dll version not supported&quot;;<br><br> hostent = gethostbyname(&quot;localhost&quot;);<br><br> pPtr = (in_addr *)(hostent-&gt;h_addr_list[0]);<br><br> result = inet_ntoa(*pPtr);<br><br> WSACleanup();<br><br> return result;<br>}<br><br>Could it possibly be the index passed with h_addr_list? How should I change it. Thanks in advance.<br><br>dex<br>
 
Dear dex,<br><br>You are right on target, just change 'localhost' to the machine name, or, more correctly use gethostname() to obtain it. NOTE: 'localhost' is defined as 127.0.0.1<br><br>Well I had to dig around in some really old stuff but here an old class for VC Socket developement.<br><br>class LocalIP<br>{<br>enum { NAMELEN = 255 };<br>&nbsp;&nbsp;CString _name;<br>&nbsp;&nbsp;CString _ip;<br>&nbsp;&nbsp;HOSTENT* _pHostent;<br><br>public:<br>&nbsp;&nbsp;LocalIP(){<br><br>&nbsp;&nbsp;&nbsp;&nbsp;LPTSTR pstr = _name.GetBufferSetLength( NAMELEN);<br>&nbsp;&nbsp;&nbsp;&nbsp;gethostname( pstr, NAMELEN);<br>&nbsp;&nbsp;&nbsp;&nbsp;_name.ReleaseBuffer();<br><br>&nbsp;&nbsp;&nbsp;&nbsp;_pHostent = gethostbyname(_name);<br>&nbsp;&nbsp;&nbsp;&nbsp;_ip = inet_ntoa(*((in_addr*)_pHostent-&gt;h_addr_list[0]));<br>&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;~LocalIP(){}<br><br>public:<br>&nbsp;&nbsp;LPCTSTR getName(){ return _name; }<br>&nbsp;&nbsp;LPCTSTR getIP(){ return _ip; }<br>};<br><br>Hope this helps<br>-pete
 
Thanks, pete. I just thought that localhost always points to the active IP of the system but obviously I was wrong.

dex
 
Hallo,

This is an old post, but almost what I'm looking for.

As I understand it, h_addr_list is a list of addresses, and in the above example you use the first.
I'm no C++ expert, so would it be possible to have the code to output the list. Failing that does anyone know the structure of h_addr_list?

When calling gethostbyname on Windows 2000 with no connected machine, h_addr_list contains 127.0.0.1, rather than the IP address set up in the TCP/IP properties (which is what I really want). I'm kind of hoping that the actual IP address is the next entry in the list

- Frink
 
Hallo again,

I've worked out how to loop through the address list, it was embarassingly simple in the end.

Unfortunately, the list only contains 127.0.0.1 on Windows 2000 so I'm still looking for a way of obtaining the system IP Address (or Default Gateway) as set up in the TCP/IP properties.

Any Clues?

- Frink
 
Frink, are you using &quot;localhost&quot;?

>> You are right on target, just change 'localhost' to the
>> machine name, or, more correctly use gethostname() to
>> obtain it. NOTE: 'localhost' is defined as 127.0.0.1


-pete
 
Hallo,

I was using gethostbyname(), but it seems that that only returns the IP Address set up in the TCP/IP properties (in Win2k) when the Network is connected. When the Network is unplugged, or not connected to anything, it only returns 127.0.0.1.
When using WinNT I get the TCP/IP address whether any network is connected or not.

I have changed my code to only connect when an appropriate TCP/IP address is available, so it works in both OS's.

Cheers,

- Frink
 
The code below will display all the ip addresses on one machine in a list box in a dgl box. If there is only one address the dlg box is not displayed (well is is but very quickly the user doesn't see it). If there is more then one ip address (more NICs) the user double clicks on the ip address he / she want to bind to. This address can be retrieve by calling GetAddress() as show below.

/////////////////////////////
//
// Using CBindAddress
//
CBindAddress* pBindAddress = new CBindAddress;
pBindAddress->DoModal();
CString local = pBindAddress->GetAddress();
delete pBindAddress;


/////////////////////////////
//
// CBindAddress dlg class
//
// BindAddress.cpp : implementation file
//

#include &quot;stdafx.h&quot;
#include &quot;IPDebugTest.h&quot;
#include &quot;BindAddress.h&quot;

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CBindAddress dialog


CBindAddress::CBindAddress(CWnd* pParent /*=NULL*/)
: CDialog(CBindAddress::IDD, pParent),
Address(&quot;NULL&quot;)
{
//{{AFX_DATA_INIT(CBindAddress)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}

void CBindAddress::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBindAddress)
DDX_Control(pDX, IDC_ADDRESSLIST, m_ctrlAddressList);
//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CBindAddress, CDialog)
//{{AFX_MSG_MAP(CBindAddress)
ON_LBN_DBLCLK(IDC_ADDRESSLIST, OnDblclkAddresslist)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBindAddress message handlers

BOOL CBindAddress::OnInitDialog()
{
CDialog::OnInitDialog();

// check for number on NIC in machine
struct in_addr addr;
struct hostent *phe;
char ac[80];
int i;
CString csLocalAddress;

// get the name of the local host
if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) {
printf(&quot;Error getting local host name: %d\n&quot;,WSAGetLastError());
return 1;
}
printf(&quot;Host name is %s\n&quot;,ac);
phe = gethostbyname(ac);
if (phe == 0) {
AfxMessageBox(&quot;Error: Bad host lookup.&quot;);
return 1;
}
for (i=0; phe->h_addr_list !=0; ++i) {
memcpy(&addr, phe->h_addr_list,sizeof(struct in_addr));
csLocalAddress = inet_ntoa(addr);
m_ctrlAddressList.AddString(csLocalAddress);
}
if(i==1){
Address = &quot;0.0.0.0&quot;;
EndDialog(0);
}
m_ctrlAddressList.AddString(&quot;0.0.0.0&quot;);

return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

void CBindAddress::OnDblclkAddresslist()
{
m_ctrlAddressList.GetText(m_ctrlAddressList.GetCurSel(),Address);
EndDialog(0);
}

CString CBindAddress::GetAddress()
{
return Address;
}


Cheers,

Brother C
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top