Why do I get a wrong address error when I try to use sendto () on my UDP server?


I am quite new to socket programming. I am programming a UDP server to follow the Time protocol. I am confused since I have done a UDP server which uses the Daytime protocol and this was successful. I only copied the code of that one and just changed the message.

    #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h>
#define PORT "6000"    // the port users will be connecting to
#define MAXBUFLEN 512

// get sockaddr, Ipv4 or Ipv6:
void *get_in_addr(struct sockaddr *sa)
    if (sa->sa_family == AF_INET) {
        return &(((struct sockaddr_in*)sa)->sin_addr);

    return &(((struct sockaddr_in6*)sa)->sin6_addr);

int main(void)
    int sockfd;
    struct addrinfo info, *udpServerInfo, *connection;
    int rv;
    int nrBytes;
    const struct sockaddr_storage their_addr;
    char buf[MAXBUFLEN];
    socklen_t addr_len;
    char s[INET6_ADDRSTRLEN];

    time_t mytime;
    mytime = time(NULL);

    memset(&info, 0, sizeof info);
    info.ai_family = AF_INET; // Ipv4 address
    info.ai_socktype = SOCK_DGRAM;
    info.ai_flags = AI_PASSIVE; // use my Ip

    if ((rv = getaddrinfo(NULL, PORT, &info, &udpServerInfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;

    // loop through all the results and bind to the first we can
    for(connection = udpServerInfo; connection != NULL; connection = connection->ai_next) {
        if ((sockfd = socket(connection->ai_family, connection->ai_socktype,
                             connection->ai_protocol)) == -1) {
            perror("listener: socket");

        if (bind(sockfd, connection->ai_addr, connection->ai_addrlen) == -1) {
            perror("listener: bind");


    if (connection == NULL) {
        fprintf(stderr, "listener: failed to bind socket\n");
        return 2;

   // printf(asctime(&mytime));


    printf("listener: waiting to recvfrom...\n");

    addr_len = sizeof their_addr;

    int done = 1;

    while (done == 1 ){

        nrBytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
                           (struct sockaddr *)&their_addr, &addr_len);

        printf("listener: got packet from %s\n",
                         get_in_addr((struct sockaddr *)&their_addr),
                         s, sizeof s));

        printf("listener: packet is %d bytes long\n", nrBytes);
        buf[nrBytes] = '\0';
        printf("listener: packet contains \"%s\"\n", buf);

        if(sendto(sockfd, mytime, sizeof(mytime), 0, (struct sockaddr *)&their_addr, addr_len) == -1){

            perror("send %s\n");


            printf ("Seconds %lu\n", mytime);
            done = 0;



    return 0;

The second parameter of sendto() requires a pointer. You should call it like: sendto(sockfd, &mytime, sizeof(mytime))