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
Solved

How to get my pc's ip?

Posted on 2006-07-16
1
181 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Complete beginner needs help making a cron job 9 125
Change owner from userA to server Local Administrators 7 66
while loop over for loop 7 115
unix example issues 18 98
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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