Thread: WinSock and gethostbyname() won't return correctly...

  1. #1
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question WinSock and gethostbyname() won't return correctly...

    I was just messing around with the WinSock function 'gethostbyname'. I did this:

    struct hostent *h;
    char lpStringIP[20];
    h = gethostbyname ("www.heartoftn.net");

    sprintf (lpStringIP, "%d.%d.%d.%d", h->h_addr_list[0][0], h->h_addr_list[0][1], h->h_addr_list[0][2], h->h_addr_list[0][3]);


    but whenever one of the "blocks" of the IP address is 200+, it returns as a negative number. With the heartoftn.net (my ISP) the first part is supposed to be 207, but it returns as: -49.65.112.11. All of it is correct except the first one. Can someone please help me?

    Thanks,
    Matt U.
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  2. #2
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Unhappy Nope...

    That gave me a HUGE number.
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    This should work for you:
    Code:
    struct hostent *h; 
    char lpStringIP[20]; 
    h = gethostbyname("www.heartoftn.net"); 
    
    sprintf (lpStringIP, "%d.%d.%d.%d", (unsigned __int8)h->h_addr_list[0][0], (unsigned __int8)h->h_addr_list[0][1], (unsigned __int8)h->h_addr_list[0][2], (unsigned __int8)h->h_addr_list[0][3]);
    All that the (unsigned __int8) does is convert it to a number between 0-255.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help problem in winsock code
    By lolguy in forum C Programming
    Replies: 8
    Last Post: 02-12-2009, 07:33 PM
  2. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  3. Resolving Hostname Using getaddrinfo(...) :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 1
    Last Post: 05-03-2002, 08:35 PM
  4. WinSock Function Locking up...
    By minime6696 in forum Windows Programming
    Replies: 1
    Last Post: 01-21-2002, 12:44 AM