Solved

How to get my pc's ip?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

930 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now