Thread: Issue with gethostbyname() calls.

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    41

    Issue with gethostbyname() calls.

    Hi everyone,

    I am writing a C program that uses sockets(UDP) to both send and receive in the same program.
    I call gethostbyname() on my client program to get the hostname and dynamic port, then i call gethostbyname() again on the server program's data. But for some reason my initial gethostbyname() call which was stored is being overwritten. I was wondering if anyone could tell me why.


    Code:
    struct hostent *hostptr;
    struct hostent *destptr;
    
    hostptr = gethostbyname( hostname );
      if ( hostptr == NULL ) 
        {
          perror( "recvUDP:gethostbyname()" );
          return -1;
        }
    
      /*
       * Bind a name to the socket
       */
      val = bind( socketFD, (struct sockaddr *)&srcAddr, (socklen_t)srclen);
      if ( val < 0 )
        {
          perror( "recvUDP:bind()" );
          return -1;
        }
    
      /* update the structure with the dynamic port number */
      int retval; /* return value for getsockname */  
      retval = getsockname(socketFD, (struct sockaddr *)&srcAddr, (socklen_t *)&srclen );
      if( retval < 0 )
        {
          perror( "recvUDP:getsockname()" );
          return -1;  
     }
    
      /*
       * Announce this programs IP and PORT
       */
      printf("Host name: %s\n", hostptr->h_name);                  !!!!!!!! This one prints the 
      printf("Host port: %d\n\n\n", ntohs(srcAddr.sin_port));    !!!!!!!! correct hostname
    
    
      //server info
      destptr = gethostbyname(argv[1]);
       if ( destptr == NULL )
      {
        perror("sendUDP:gethostbyname()");
        return -1;
      }
      
    
    
      /*
       * Announce this programs IP and PORT
       */
      printf("Host name: %s\n", hostptr->h_name);    !!! This one is incorrect
      printf("Host port: %d\n", ntohs(srcAddr.sin_port));
    
      //Annouce server's IP and PORT
      printf("Server name: %s\n", destptr->h_name);
      printf("Server port: %d\n", ntohs(destAddr.sin_port));

    I'm pretty sure my second call to gethostbyname() is overwriting the first, i just dont know why. Any help would be great. This is for an assignment so please no solutions, just hints.

    Thanks.
    Hunter

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Check the RETURN VALUE and NOTES section of the man page for gethostbyname.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    41
    Thanks for your prompt reply, the man pages on our server doesnt have the notes section for some reason.

    So to how would I perform a deep copy, would I have to copy each member of the struct into a corresponding struct?

    i.e.
    Code:
    struct hostent *hostptr;
    struct hostent *temp;
    temp = gethostbyname();
    
    for all member of temp:
          hostptr.member_name = temp.member_name;
    end for
    ?

    Thanks.
    Hunter

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    That's the right idea, though memcpy would be easier. The only problem I forsee is if the pointer members of a hostent also refer to static variables, in which case simple address assignment wouldn't work.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    41
    hmm well I'm running into trouble with memcpy, this is what I have tried:

    Code:
      memcpy(&(destptr->h_name), (int *)tempptr->h_name, sizeof(char *));
      memcpy(&(destptr->h_length), (int *)tempptr->h_length, sizeof(int));
      memcpy(&(destptr->h_addr), tempptr->h_addr, sizeof(char *));
    
     and
    
      memcpy(destptr->h_name, tempptr->h_name, sizeof(char *));
      memcpy(destptr->h_length, tempptr->h_length, sizeof(int));
      memcpy(destptr->h_addr, tempptr->h_addr, sizeof(char *));
    neither of which seem to be working.
    any ideas?

    Thanks,
    Hunter

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I didn't mean memcpy each element. Copy the whole struct, so the src address for memcpy is the address returned by gethostbyname. The dest is whatever you declare to hold the data, and the size is sizeof(hostent).

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    41
    hmm copying the whole structure like you suggested is causing a segmentation fault.

    Code:
    /*
       * Get DNS information about this hosts's name
       */
      memcpy(hostptr, gethostbyname(hostname), sizeof(struct hostent));
      if (hostptr == NULL) 
        {
          perror( "recvUDP:gethostbyname()" );
          return -1;
        }
    
     
      //server info
      /*
       * Get DNS information for the destination hosts name
       */
    
      memcpy(destptr, gethostbyname(argv[1]), sizeof(struct hostent));
      if ( destptr == NULL )
        {
          perror( "recvUDP:gethostbyname()" );
          return -1;
        }
    I have intently looked at the file, and can't find anyplace where I am accessing memory out of turn.

    Hunter

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You need actual space reserved for the memcpy. hostptr is just a pointer (4 bytes on a 32-bit system), and probably an uninitialized one at that, meaning you're copying too much data into god knows where. You need a hostent object, and memcpy to the address, like so:
    Code:
    hostent host;
    memcpy(&host, ...
    Note that gethostbyname could return NULL, so you should check for that before memcpy'ing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. CoInitialize[ex]/CoUnitialize calls
    By @nthony in forum Windows Programming
    Replies: 4
    Last Post: 07-24-2009, 11:17 AM
  3. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  4. Weird gethostbyname() issue
    By Xterria in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-26-2006, 09:44 PM
  5. C/C++ Memory Calls!
    By Joda in forum C++ Programming
    Replies: 7
    Last Post: 10-25-2002, 04:50 PM