Winsock: GetHostByAddr

I'm writing a simple program to convert an IP address to a hostname, but I'm having trouble with the function GetHostByAddr (found in winsock.h (windows) or netdb.h (linux/unix)). The function takes three arguments (const char FAR * addr,int len, int type) len=4 and type=AF_INET. I've tried simply passing a string (ie: "127.0.0.1") as the addr argument, but it doesn't work. I think it wants the address in network byte order but I can't figure it out. If you have an idea what the problem is (or source code, or a suggestion) please tell me. I will post my source in my next message.


Comments


  • #include



    #include



    #include












    void main(int argc, char * argv[])



    {







       if(argc==2)



        {



            WSADATA wsaData;







           if(WSAStartup(MAKEWORD( 1, 1 ),

    &wsaData)==0)



           {



               

    /* Define addr here */




                struct

    hostent *hostinfo=gethostbyaddr(addr, 4, AF_INET);







               if

    (hostinfo!=NULL)



                {



                   

    printf("%s", hostinfo->h_name);



                 }



                else



                {



                   

    printf("%s", "error");



                }



            }



            getche();



        }



    }


  • Damn HTML parsing!


    #include

    #include

    #include




    void main(int argc, char * argv[])

    {

    if (argc==2)

    {

    WSADATA wsaData;

    if (WSAStartup(MAKEWORD( 1, 1 ), &wsaData)==0)

    {

    /* Define addr here */

    struct hostent *hostinfo=gethostbyaddr(addr, 4, AF_INET);

    if (hostinfo!=NULL)

    {

    printf("%s", hostinfo->h_name);

    }

    else

    {

    printf("%s", "error");

    }

    }

    getche();

    }

    }


  • includes:

    stdio.h

    conio.h

    winsock.h


  • includes:

    stdio.h

    conio.h

    winsock.h


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories