No announcement yet.

Using "gethostbyname()"

  • Filter
  • Time
  • Show
Clear All
new posts

  • Using "gethostbyname()"

    Dear all:

    Maybe this is a simple question but I'm having lot of problems when trying to use gethostbyname() to find a remote server.

    The function seems to work correctly but I don't know how to use the returning value "char∗∗ hostent::h_addr_list"

    1. How do I know the ammount of addresses that the function has returned in the h_addr_list? (sizeof(h_addr_list) does not work in this case)

    2. How do I use the stored address in the connect() function? Connect() expects a sockaddr struct but I'm just getting a long uint where h_addr_list points to.

    3. Does anyone have an example?

    Thanks in advance.

    PS: I forgot a remark. I'm using Xport PRO
    Last edited by dreuu; 09-18-2010, 11:34 AM. Reason: I did not specify the device I'm using
    XPort Pro newbie

  • #2
    gethostbyname returns lists

    There are two lists returned: h_aliases and h_addr_list. Each is an array of pointers. Each array ends with a NULL (0) pointer.


    • #3
      using connect()

      See the SDK sample code aestunnelclient.c in the configureClient routine for an example of usage of connect(). Notice that a cast must be used. I don't like it, but that's the tradition.


      • #4
        Done but...

        Well, It seems that I've made it work but I'm not really sure this is the best way to code it:

        struct hostent servidor; //Hostent Definition. Global
        char bufferHost[128]; //Buffer for the function. Global
        //Inside my main thread:
        if(gethostbyname (hostname, &servidor, bufferHost, sizeof(bufferHost)) == 0 ){
        		TLOG(TLOG_SEVERITY_ERR, "Error...");
        } else{
                        memset(&servername, 0, sizeof(servername));
        		servername.sin_family = AF_INET;
        		servername.sin_port = htons (PORT);
        //I've used snprintf to translate the given address. "sms" is a buffer.
                        (servidor.h_addr_list[0]+3) );
        		servername.sin_addr.s_addr = 
                        NetDottedTo32BitNetworkByteOrder(sms);  // IP 
        It works but I'm still having problems when trying to read the size of "h_aliases" and "h_addr_list". SizeOf and Strlen complain with warnings when compiling.

        I would like to make a better usage of the function allowing multiple addresses to be check and aliases. Also, I'm sure there's a better way to read the address instead of snprintf.
        XPort Pro newbie


        • #5
          Mind, that ".h_aliases" and ".h_addr_list" are lists of pointers. The ".aliases" is char**-type; so ".h_aliases[0]" is a pointer to a NULL-terminated textual description somewhere else in the memory. This works for other elements, too. But mind that this list of pointers is terminated by a NULL pointer. Sizeof() and strlen() won't work; I think you have to look at every Pointer one by one.

          The same rules apply for the ".h_addr_list" with some difference. The target of the pointers are binary coded address-data (and probably without NULL termination).

          Maybe this helps: