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