?
Solved

IP address of a Host

Posted on 2001-06-06
9
Medium Priority
?
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

752 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