c - 为什么在我的 UDP 服务器上尝试使用 sendto() 时会收到错误的地址错误?

标签 c linux sockets udp

我对套接字编程很陌生。我正在编写一个 UDP 服务器以遵循时间协议(protocol)。我很困惑,因为我已经完成了一个使用 Daytime 协议(protocol)的 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;
}

最佳答案

sendto() 的第二个参数需要一个指针。你应该这样调用它: sendto(sockfd, &mytime, sizeof(mytime))

关于c - 为什么在我的 UDP 服务器上尝试使用 sendto() 时会收到错误的地址错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39944227/

相关文章:

坐标传递给 XDrawImageString()

c++ - boost::匿名段上的进程间内存分配器

linux - 从 crontab 设置自动 rsync 以通过互联网将内容从一台 Linux 机器同步到另一台 Linux 机器的 final方法是什么?

linux - bind() before connect() 可能会返回 EADDRNOTAVAIL,即使有许多临时端口可用

python - argv 的 ctypes 错误

c++ - 当我将它添加到指针时,我可以安全地忽略溢出检查的最小偏移量是多少?

c - 删除某个值多次出现的链表

c - 相当简单的 C 代码中的安全漏洞

c - nftw 目录的总大小与 du 输出不同

c++ - 如何在 C++ 中使用套接字 (UDP) 在 LAN 上查找服务器