Solved

IP address of a Host

Posted on 2001-06-06
9
179 Views
Last Modified: 2013-11-20
hi all
i want to get the IP address of a host connected in a network.
i tried a sample program but it gives me an error
saying error getting local hostname.
i want to use it my application.
 for testing purpose i wrote in C.


can any one give the solution to my problem


#include <iostream.h>
#include <Winsock2.h>
 
int main()
{
struct in_addr addr;
   struct hostent *phe;
   char ac[80];
   int i;
   char  *local;
   
   // get the name of the local host
  if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) {
      printf("Error getting local host name: %d\n",WSAGetLastError());
      return 1;
   }
   printf("Host name is %s\n",ac);
   
   phe = gethostbyname(ac);  
   if (phe == 0) {
      cout << "Error:  Bad host lookup." << endl;        
   return 1;
   }    
   for (i=0; phe->h_addr_list[i] !=0; ++i) {        
      memcpy(&addr, phe->h_addr_list[i],sizeof(struct in_addr));
      printf("Card %i) %s\n",i,inet_ntoa(addr));
      local=inet_ntoa(addr);
   }  
}
0
Comment
Question by:navyareddy
  • 8
9 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 30 total points
ID: 6159095
You are NOT checkeding the ERROR code returned by the gethostname() function.  If you did, you'd see that it is saying WSANOTINITIALISED.  This means:

A successful WSAStartup call must occur before using this function. (to quote the SDK)

Call WSAStartup() BEFORE calling the gethostname function and call WSACleanup BEFORE ending your application.

Try:

#include <iostream.h>
#include <Winsock2.h>
 
int main()
{
struct in_addr addr;
  struct hostent *phe;
  char ac[80];
  int i;
  char  *local;

  WORD wVer = MAKEWORD(1, 1);
  WSADATA ws;


  if (WSAStartup(wVer, &ws) != 0){
    printf("Error in WSAStartup\n");
    return 1;
  }
 
  // get the name of the local host
 if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) {
     printf("Error getting local host name: %d\n",WSAGetLastError());
     WSACleanup;
     return 1;
  }
  printf("Host name is %s\n",ac);
 
  phe = gethostbyname(ac);  
  if (phe == 0) {
     cout << "Error:  Bad host lookup." << endl;        
  return 1;
  }    
  for (i=0; phe->h_addr_list[i] !=0; ++i) {        
     memcpy(&addr, phe->h_addr_list[i],sizeof(struct in_addr));
     printf("Card %i) %s\n",i,inet_ntoa(addr));
     local=inet_ntoa(addr);
  }  
  WSACleanup()
}
0
 

Author Comment

by:navyareddy
ID: 6159312
thanks its working fine.
0
 
LVL 32

Expert Comment

by:jhance
ID: 6159645
>>thanks its working fine.

In that case, it's customary to ACCEPT the answer and close this question.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:jhance
ID: 6161292
Hello????
0
 
LVL 32

Expert Comment

by:jhance
ID: 6161833
Hello?
0
 
LVL 32

Expert Comment

by:jhance
ID: 6162276
Hello?????
0
 
LVL 32

Expert Comment

by:jhance
ID: 6163283
Hello????????????????????????????????????????????????????????????????????????????????????/
0
 
LVL 32

Expert Comment

by:jhance
ID: 6165084
Hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello, hello
0
 
LVL 32

Expert Comment

by:jhance
ID: 6167461
>>>and have been rewarded with a grade of B.

What an INGRATE!!!  Not only did I explain what you were doing wrong I correct your code!!!

Don't expect to receive help from me in the future!@
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.

756 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