Reputation: 56
I'm writing in STM32CubeIDE v1.15.0 for a Nucleo-F756ZG I want to get a timestamp from a NTP server and I found a guide with a link to this code: https://github.com/lettier/ntpclient/blob/master/source/c/main.c
struct sockaddr_in serv_addr; // Server address data structure.
struct hostent \*server; // Server data structure.
int sockfd = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP ); // Create a UDP socket.
if ( sockfd < 0 )
printf( "ERROR opening socket" );
server = gethostbyname( "pool.ntp.org" ); // Convert URL to IP.
if ( server == NULL )
printf( "ERROR, no such host" );
// Zero out the server address structure.
bzero( ( char* ) &serv_addr, sizeof( serv_addr ) );
serv_addr.sin_family = AF_INET;
// Copy the server's IP address to the server address structure.
bcopy( ( char* )server->h_addr, ( char* ) &serv_addr.sin_addr.s_addr, server->h_length );
// Convert the port number integer to network big-endian style and save it to the server address structure.
int portno = 123; // NTP UDP port number.
serv_addr.sin_port = htons( portno );
The problem that I have is that I do not understand why here bcopy( ( char\* )server-\>h_addr, ( char\* ) &serv_addr.sin_addr.s_addr, server-\>h_length );
it gives me this error invalid use of undefined type 'struct hostent'
while there is not a problem here server = gethostbyname( "pool.ntp.org" ); // Convert URL to IP
I included the netdb.h library and is not giving me any errors there so that s a confusing part for me cuz I thought it might be a linker error.
My question is: what is the source of this problem? how can I fix it? OR if you can point me to another implementation of getting a timestamp from a NTP server.
I tried changing the bcopy function to memcpy(&serv_addr.sin_addr.s_addr, server-\>h_addr, server-\>h_length);
, but it didn't do nothing, I tried to move the #include \<netdb.h\>
in every file that made sense
Upvotes: 0
Views: 82