Solved

How to get my pc's ip?

Posted on 2006-07-16
1
182 Views
Last Modified: 2013-11-20
I am using ADSL connection, the ip may change, my program need to check for fixed time interval of the pc's ip.The OS is xp
Is there any sample code?

Then I need to write it to a log file and write to DB.

Thx
0
Comment
Question by:turbot_yu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 17117464
This is pretty straightfoward using 'gethostbyname()' with your local name, e.g.

  char                szErrorMessage[ 129 ];
  char                szLocalHostName[ 129 ];
  unsigned long       ulInetAddr;
  struct sockaddr_in  sin;
  struct hostent      *pHostEnt;
  int                 nRC;

  WORD wVersionRequested;
  WSADATA wsaData;
  int err;
 
  wVersionRequested = MAKEWORD( 2, 2 );
 
  err = WSAStartup( wVersionRequested, &wsaData );

  nRC = gethostname( szLocalHostName, sizeof( szLocalHostName ) );

  if ( nRC == -1 )
    {
    sprintf( szErrorMessage, "gethostname() failed.  errno=[%d]", errno);

    perror( szErrorMessage );

    exit( EXIT_FAILURE );
    } // end if

  fprintf( stdout, "gethostname() returned %d characters.\n", nRC );
  fprintf( stdout, "  Name        = [%s]\n", szLocalHostName );

  if ( ( ulInetAddr = inet_addr( szLocalHostName ) ) == ((unsigned long)-1L) )
    {
    if ( ( pHostEnt = gethostbyname( szLocalHostName ) ) == NULL )
      {
      sprintf( szErrorMessage, "gethostbyname() failed.  errno=[%d]",errno );

      perror( szErrorMessage );

      exit( EXIT_FAILURE );
      } // end if

    memcpy( (char *)&sin.sin_addr, (char *)pHostEnt->h_addr,pHostEnt->h_length);
    }
  else
    {
    memcpy( (char *)&sin.sin_addr, (char *)&ulInetAddr, sizeof(ulInetAddr ));

  } // end if

  fprintf( stdout, "  Dot Address = [%s]\n", inet_ntoa( sin.sin_addr ) );

  WSAShutdown();

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error on moodle after upgrade 3 149
x-bar in Google Sheets 2 91
Bartender label printing - switch on and off graphics 3 71
How do I get Window Title of all opened process? 4 33
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

762 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