Thread: gethostbyaddr Help Plz

  1. #1
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339

    Cool gethostbyaddr Help Plz

    Hi,

    Can someone give me an example of using gethostbyaddr to convert and IP to a hostname then display it in a messagebox.
    I have done this to convert a hostname to an IP:

    h = gethostbyname("modem-537.****covered_lol");

    MessageBox(hWnd, inet_ntoa(*((struct in_addr *)h->h_addr)), "IP Address", NULL);

    This works fine and converts the hostname to an IP.

    But i cant work out how to do it the other way round, and convert an IP to a hostname.

    Thanks in advance
    TNT
    You Can Stop Me, But You Cant Stop Us All

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Hm.. Might be wrong, but I dont think it works that way??

  3. #3
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    Create an in_addr struct to hold the IP address, send it to gethostbyaddr(), and save the pointer to the returned HOSTENT struct:

    Code:
    in_addr toBeResolved;
    toBeResolved.s_addr = inet_addr("127.0.0.1");
    
    LPHOSTENT hasBeenResolved;
    hasBeenResolved = gethostbyaddr((const char *)&toBeResolved, sizeof(struct in_addr), AF_INET);
    At this point, you have the hostname of the computer in the LPHOSTENT struct.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. plz help me...
    By sweetchakri in forum C Programming
    Replies: 1
    Last Post: 03-03-2009, 11:50 PM
  3. [Request] Need Help Plz
    By TylerD in forum Tech Board
    Replies: 4
    Last Post: 01-03-2009, 09:54 AM
  4. Anyone plz help me
    By Rose_Flowers in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-17-2003, 12:01 PM
  5. help plz plz
    By nsssn73 in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 08:44 AM