?
Solved

Getting the ip.

Posted on 2007-07-30
13
Medium Priority
?
303 Views
Last Modified: 2010-04-01
I need the most simple way to determine the computers IP address and if possible the hostname.
Using Borland C++ Builder 6.0.

Urgent. Got to finish this project tonight!
0
Comment
Question by:Jammer1224
  • 9
  • 4
13 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 1000 total points
ID: 19594518
See http://www.codeproject.com/internet/my_ip_-_v2.asp ("Getting Addresses IP informations") - the scoop is to

    int     iResult;
    DWORD   dwOutBufLen;
    char szHostName[256];

    char    szTmp[256];

    WSADATA WsaData;

    struct hostent* lpHostEnt;

    iResult = WSAStartup(MAKEWORD(2,2),&WsaData);
    if(iResult)
    {
        // Can't call WSAGetLastError() because WinSock DLL is not loaded !

        wsprintf(szTmp,"WSAStartup failed, returned error code : %d",iResult);
        MessageBox(NULL,szTmp,szError,MB_OK|MB_ICONEXCLAMATION);
        return (FALSE);
    }

    memset(szHostName,0,sizeof(szHostName));


    // Getting hostname
    if(gethostname(szHostName,sizeof(szHostName)) == SOCKET_ERROR)    
     return (ShowNetworkError("gethostname failed, returned error code : %d"));

    // Get IP address
    lpHostEnt = gethostbyname(szHostName);
    if(!lpHostEnt)    
     return (
     ShowNetworkError("gethostbyname failed, returned error code : %d"));

    // IP address is stored in 'lpHostEnt->h_addr
0
 
LVL 86

Expert Comment

by:jkr
ID: 19594534
Or, alternatively, a stripped down version from my archives:

char acClientHost[255];
WORD wVersionRequested;
WSADATA wsaData;
struct hostent *pHostent = NULL;
 
wVersionRequested = MAKEWORD( 1, 1);

if ( nErr = WSAStartup ( wVersionRequested, &wsaData))
{
  // error initializing winsck...
}

    if  (   dwErr =   gethostname (   ( char *) acClientHost, 255))
        {
            return  (   nWSockRC);
        }

if (!(pHostent = gethostbyname ( acClientHost)))
{
 // an error occured...
}
else
{
 printf ( "IP: %s\n", BinIP2StrIP ( *(( long *)  pHostent->h_addr));
}


char *BinIP2StrIP ( long lnIP)
{
static char s_acStrIP [ 16];

PUCHAR puc = ( unsigned char *) &lnIP;
unsigned int aun [ 4];

for ( int i = 0; i < 4; aun [ i++] = *puc++);

sprintf ( s_acStrIP, "%d.%d.%d.%d", aun [ 0], aun [ 1], aun [ 2], aun [ 3]);

return ( s_acStrIP);
}

Link with ws2_32.lib and add '#include <winsock.h>' to your source file.
0
 
LVL 86

Expert Comment

by:jkr
ID: 19594538
And your question is covered in detail here (no, I didn't just copy it ;-)

http://www.codeguru.com/network/local_hostname.shtml
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Jammer1224
ID: 19594545
Oh my god! Isn't there anything like GetIpAddress? :D
0
 
LVL 86

Expert Comment

by:jkr
ID: 19594566
Well, just make that

char* GetIpAddress()
{

}char acClientHost[255];
WORD wVersionRequested;
WSADATA wsaData;
struct hostent *pHostent = NULL;
 
wVersionRequested = MAKEWORD( 1, 1);

if ( nErr = WSAStartup ( wVersionRequested, &wsaData))
{
  // error initializing winsck...
}

    if  (   dwErr =   gethostname (   ( char *) acClientHost, 255))
        {
            return  (   nWSockRC);
        }

if (!(pHostent = gethostbyname ( acClientHost)))
{
 // an error occured...
}

return BinIP2StrIP ( *(( long *)  pHostent->h_addr);
}

;o)
0
 

Author Comment

by:Jammer1224
ID: 19594613
[C++ Error] Unit1.cpp(41): E2451 Undefined symbol 'nErr'
[C++ Error] Unit1.cpp(46): E2451 Undefined symbol 'dwErr'
[C++ Error] Unit1.cpp(48): E2451 Undefined symbol 'nWSockRC'
0
 
LVL 86

Expert Comment

by:jkr
ID: 19594658
OK, cleaning that up a bit:

char *BinIP2StrIP ( long lnIP)
{
static char s_acStrIP [ 16];

PUCHAR puc = ( unsigned char *) &lnIP;
unsigned int aun [ 4];

for ( int i = 0; i < 4; aun [ i++] = *puc++);

sprintf ( s_acStrIP, "%d.%d.%d.%d", aun [ 0], aun [ 1], aun [ 2], aun [ 3]);

return ( s_acStrIP);
}

char* GetIpAddress()
{

char acClientHost[255];
WORD wVersionRequested;
WSADATA wsaData;
struct hostent *pHostent = NULL;
int nErr = 0;
 
wVersionRequested = MAKEWORD( 1, 1);

if ( nErr = WSAStartup ( wVersionRequested, &wsaData))
{
  // error initializing winsck...
}

if  (   nErr =   gethostname (   ( char *) acClientHost, sizeof(acClientHost)))
{
     return  NULL;
}

if (!(pHostent = gethostbyname ( acClientHost)))
{
 // an error occured...
  return NULL
}

return BinIP2StrIP ( *(( long *)  pHostent->h_addr);
}
0
 

Author Comment

by:Jammer1224
ID: 19594755
first: forgot ; after first from the end NULL
misses one ) at the last return

second:
PUCHAR puc = ( unsigned char *) &lnIP;
maby its char * .... if it is ..
for ( int i = 0; i < 4; aun [ i++] = *puc++)
here we have an expression syntax error.
trying
int i = 0;
for(;i < 4;aun[i++] = *puc++)

then get a declaration syntax error not even at your code
0
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 19594789
Sorry ;o)

This one compiles and runs:

#include <windows.h>
#include <winsock.h>
#include <stdio.h>

char *BinIP2StrIP ( long lnIP)
{
static char s_acStrIP [ 16];

PUCHAR puc = ( unsigned char *) &lnIP;
unsigned int aun [ 4];

for ( int i = 0; i < 4; aun [ i++] = *puc++);

sprintf ( s_acStrIP, "%d.%d.%d.%d", aun [ 0], aun [ 1], aun [ 2], aun [ 3]);

return ( s_acStrIP);
}

char* GetIpAddress()
{

char acClientHost[255];
WORD wVersionRequested;
WSADATA wsaData;
struct hostent *pHostent = NULL;
int nErr = 0;
 
wVersionRequested = MAKEWORD( 1, 1);

if ( nErr = WSAStartup ( wVersionRequested, &wsaData))
{
  // error initializing winsck...
}

if  (   nErr =   gethostname (   ( char *) acClientHost, sizeof(acClientHost)))
{
     return  NULL;
}

if (!(pHostent = gethostbyname ( acClientHost)))
{
 // an error occured...
  return NULL;
}

return BinIP2StrIP ( *(( long *)  pHostent->h_addr));
}

int main ()
{
  printf("IPAddr: %s\n", GetIpAddress());

  return 0;
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 19594803
One little correction - add

WSACleanup();

before

return BinIP2StrIP ( *(( long *)  pHostent->h_addr));
0
 

Author Comment

by:Jammer1224
ID: 19594827
finnaly! :D
0
 
LVL 86

Expert Comment

by:jkr
ID: 19594880
Um, thanks, but why a "C"? http://www.experts-exchange.com/Community_Support/help.jsp#hi73

If you have given the Expert(s) ample time to respond to your clarification posts and you have responded to each of their posts providing requested information; or if the answers, after clarification, lack finality or do not completely address the issue presented, then a "C" grade is an option.


That grade usually classifies "crap" answers.
0
 
LVL 86

Expert Comment

by:jkr
ID: 19602335
No response is also a response. Well, there seem to be different types of EE users...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

840 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