Getting the ip.

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!
Jammer1224Asked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
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
 
jkrConnect With a Mentor Commented:
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
 
jkrCommented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

http://www.codeguru.com/network/local_hostname.shtml
0
 
Jammer1224Author Commented:
Oh my god! Isn't there anything like GetIpAddress? :D
0
 
jkrCommented:
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
 
Jammer1224Author Commented:
[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
 
jkrCommented:
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
 
Jammer1224Author Commented:
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
 
jkrCommented:
One little correction - add

WSACleanup();

before

return BinIP2StrIP ( *(( long *)  pHostent->h_addr));
0
 
Jammer1224Author Commented:
finnaly! :D
0
 
jkrCommented:
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
 
jkrCommented:
No response is also a response. Well, there seem to be different types of EE users...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.