Outputting your IP and hostname

This is a discussion on Outputting your IP and hostname within the C++ Programming forums, part of the General Programming Boards category; How would one output your IP and hostname?...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    155

    Outputting your IP and hostname

    How would one output your IP and hostname?

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: Outputting your IP and hostname

    Originally posted by Nakeerb
    How would one output your IP and hostname?
    cout << "127.0.0.1 localhost" << endl;
    hello, internet!

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718

    Re: Re: Outputting your IP and hostname

    Originally posted by moi
    cout << "127.0.0.1 localhost" << endl;
    he speaks the truth.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    I meant using Winsock

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    That's the loopback address on any computer.



    Code:
    
    char * local_host_info(char buffer[]) {
      int index = 0;
      const int max = 200;
      char local[max];
      hostent * find;
      int result;
    
      buffer[0] = 0;
      strcat( buffer, "Local host name: " );
      result = gethostname( local, max );
      find = gethostbyname( local );
    
      if( !find || result == SOCKET_ERROR ) {
          strcat( buffer, "Information not Available.\n" );
          return buffer;
         }
    
      strcat( buffer, local );
    
      while( find->h_addr_list[ index ] ) {
        strcat( buffer, "\n" );
        strcat( buffer, inet_ntoa( *(in_addr*)find->h_addr_list[ index++ ] ) );
       }
    
     return strcat( buffer, "\n\n\n");
    }
    
    
    
    
    int main() {
    
    char buff[256]
    
    //...initialize winsock...
    
    printf("My info \n\n%s", local_host_info( buff ));
    
    return 0;
    }



    ITSA
    Socket Library!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting ip by hostname
    By NiSA in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-26-2007, 10:56 PM
  2. obtain hostname or IP
    By afisher in forum Networking/Device Communication
    Replies: 3
    Last Post: 12-01-2004, 06:46 AM
  3. ip or hostname
    By jchanwh in forum C Programming
    Replies: 2
    Last Post: 04-08-2002, 08:41 PM
  4. Resolving Hostname :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 04-08-2002, 08:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21