CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Variable type

  1. #1
    Join Date
    Oct 2010
    Posts
    46

    Variable type

    Hello. I just started using WinSock and i was using this guide for creating client:
    http://www.codeproject.com/KB/IP/winsockintro02.aspx
    Everything seems pretty easy in this guide exept one thing... What type of variable should i use for 'addr'?
    Code:
    if(inet_addr(servername)==INADDR_NONE)
    {
        hp=gethostbyname(servername);
    }
    else
    {
        addr=inet_addr(servername);
        hp=gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);
    }
    if(hp==NULL)
    {
        closesocket(conn);
        return;
    }
    Thank you

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Variable type


  3. #3
    Join Date
    Oct 2010
    Posts
    46

    Re: Variable type

    I tried this
    Code:
    const char addr[13] = "192.168.1.65";
    hostent *hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
    gethostbyaddr returned 0. Maybe i'm doing something wrong?

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Variable type

    Did you call WSAStartup?
    Victor Nijegorodov

  5. #5
    Join Date
    Oct 2010
    Posts
    46

    Re: Variable type

    Yes. I call it at the begining of my code
    Code:
    WSADATA wsaData;
    int wsaret = WSAStartup(0x101, &wsaData);
    also, WSAGetLastError returned 11004 - WSANO_DATA
    http://msdn.microsoft.com/en-us/library/ms740668
    It seems that gethostbyaddr founds addres, but has no data about it? Maybe its because 192.168.1.65 is in my local network?

    EDIT
    Its same when i changed ip to my gateway ip...

  6. #6
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Variable type

    According to http://msdn.microsoft.com/en-us/libr...21(VS.85).aspx gethostbyname is deprecated and getnameinfo should be used instead. In the remarks for getnameinfo, http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx, the name lookup is described as being either a reverse DNS or local hosts file lookup result i.e. without having a DNS entry or a hosts file entry it will fail. Try using 127.0.0.1 instead and you probably will get localhost as the result
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  7. #7
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Variable type

    inet_addr returns an unsigned long value.

  8. #8
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Variable type

    Quote Originally Posted by itsmeandnobodyelse View Post
    inet_addr returns an unsigned long value.
    First param to gethostbyaddr should be 'A pointer to an address in network byte order' and that's what inet_addr returns so that should be alright.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured