Solved

IP address of a Host

Posted on 2001-06-06
9
168 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
 
LVL 32

Expert Comment

by:jhance
ID: 6161292
Hello????
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

21 Experts available now in Live!

Get 1:1 Help Now