尝试在我的UDP服务器上使用sendto()时,为什么会出现错误的地址错误?

时间:2016-10-09 13:34:59

标签: c linux sockets udp

我对套接字编程很新。我正在编程UDP服务器以遵循时间协议。我很困惑,因为我已经完成了使用Daytime协议的UDP服务器,这很成功。我只复制了那个代码,只是更改了消息。

    #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");
            continue;
        }

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

        break;
    }

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

   // printf(asctime(&mytime));

    freeaddrinfo(udpServerInfo);

    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",
               inet_ntop(their_addr.ss_family,
                         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");

        }
        else{

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



    }

    close(sockfd);

    return 0;
   }

2 个答案:

答案 0 :(得分:0)

sendto()的第二个参数需要一个指针。你应该这样称呼: sendto(sockfd,&amp; mytime,sizeof(mytime))

答案 1 :(得分:0)

sendto()期望指向数据的指针作为第二个参数发送。

所以这个

if (sendto(sockfd, mytime, ...

应该是

if (sendto(sockfd, &mytime, ...

按照目前的情况,mytime的值将被视为地址,这很可能是一个无效的地址,这反过来会在被引用时引发sendto()内的未定义行为。

编译器应该警告过你。