Solved

IP address of a Host

Posted on 2001-06-06
9
174 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
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.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

815 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