Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

IP address of a Host

Posted on 2001-06-06
9
Medium Priority
?
185 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 90 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
Technology Partners: 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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…
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.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

876 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