Even my lecturer can't figure this 1 out.... help! Any resolver gurus
know what's happening?
I've got 2 samples. 1st sample was written by my lecturer to display the
contents of hosent structure. The 2nd sample is my game with his code
(juz that 1 line with gethostbyname()) ported over. It works perfectly
fine with the 1st sample - any text string gets accepted by
gethostbyname() causes segmentation fault in the 2nd sample though.
I've tried other alternative ways like assigning to a new char ptr,
copying to a new buffer space, & even hardcoding the string into the
function call. all will still gimme segmentation fault. The error
pattern I noticed is strings above 11 or 12 chars will produce the fault
"brady.sgt.g" will be accepted & actually queries the DNS server (I can
see it thru the modem). This is generate the "Server host invalid" msg
"brady.sgt.gm" will cause segmentation fault immediately, without
sending any DNS query packet.
if I try "188.8.131.52" gethostbyname() accepts it, but returns invalid
host. I thought gethostbyname() is able to return a hostent struct with
the same addr? giving "184.108.40.206" will produce segmentation fault
again, & "220.127.116.11" will get invalid host, & so on.
Anybody knows how gethostbyname manipulates the string?
int main(int argc, char *argv)
char *ptr, **pptr, str[INET_ADDRSTRLEN];
struct hostent *hptr;
while (--argc > 0)
ptr = *++argv;
if ((hptr = gethostbyname(ptr)) == NULL)
fprintf(stderr, " not valid hostname?\n", ptr);
printf(" official hostname : %s\n", hptr->h_name);
void start_player(char name[NAME + 1], char *servername, uint16_t port,
struct sockaddr_in serversock;
struct hostent *serverhost;
/* test param string */
printf("HMS %s to enter %s:%d at %d long %d lat\n", name, servername,
port, pos, pos);
if ((serverhost = gethostbyname(servername)) == NULL)
fprintf(stderr, "Server host invalid.\n");
/* test string */
printf("Set server socket now...\n");
inet_pton(AF_INET, serverhost->h_addr_list, &serversock.sin_addr);
serversock.sin_family = AF_INET;
serversock.sin_port = htons(port);