Solved

How to get my pc's ip?

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Show shut-down message as Windows 8 shuts down. 9 74
Perl Awk Need Help 3 95
bigDiff challenge 17 75
EvenOdd challenge 10 83
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.

743 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

12 Experts available now in Live!

Get 1:1 Help Now