Solved

How to get my pc's ip?

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Installing Microsoft R server on REDHAT or SUSE Linux 4 136
Shell script to login to remote m/c and execute commands 10 78
haveThree challenge 22 116
NotAlone Challenge 20 83
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

785 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