套接字sendto()在上次成功调用后返回EINVAL

时间:2018-02-11 19:10:42

标签: c sockets udp datagram sendto

我修改了this C数据报(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 <signal.h>

#define SERVERPORT "4950"

volatile sig_atomic_t is_shutdown = 0;

void handler_signal(int signal)
{
    if (signal == SIGINT)
    {
        is_shutdown = 1;
    }
}

int main(int argc, char *argv[])
{
    int socket_fd;
    struct addrinfo hints, *server_info, *temp;
    int result;
    int number_bytes;

    if (argc != 2)
    {
        fprintf(stderr, "usage: executable [hostname]\n");
        return 1;
    }

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_DGRAM;

    result = getaddrinfo(argv[1], SERVERPORT, &hints, &server_info);

    if (result != 0)
    {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(result));
        return 1;
    }

    // loop through all the results and make a socket
    for(temp = server_info; temp != NULL; temp = temp->ai_next)
    {
        socket_fd = socket(temp->ai_family, temp->ai_socktype, temp->ai_protocol);

        if (socket_fd == -1)
        {
            fprintf(stderr, "socket error: %s\n", strerror(errno));
            continue;
        }

        break;
    }

    freeaddrinfo(server_info);

    if (temp == NULL)
    {
        fprintf(stderr, "failed to create socket\n");
        return 2;
    }

    //int hostname_length = strlen(argv[2]);
    unsigned long counter = 0;
    const size_t buffer_length = 50;
    char buffer[buffer_length];
    memset(&buffer, 0, buffer_length);

    while (!is_shutdown)
    {
        snprintf(buffer, buffer_length, "%lu", counter++);

        number_bytes = sendto(socket_fd, buffer, buffer_length, 0, temp->ai_addr, temp->ai_addrlen);

        if (number_bytes == -1)
        {
            fprintf(stderr, "sendto error: %s\n", strerror(errno));
            break;
        }

        printf("sent %d bytes to %s.\n", number_bytes, argv[1]);
    }

    result = close(socket_fd);

    if (result == -1)
    {
        fprintf(stderr, "close error: %s\n", strerror(errno));
    }

    printf("exiting application\n");

    return 0;
}

在循环的第二次迭代中,sendto函数与Invalid argument (errno 22)失败。它适用于第一个。

但是,如果我将代码更改为以下代码,则代码可以正常运行(客户端会继续以无限循环方式发送消息,服务器会成功接收消息):

    //...

    //create a copy to pass it later
    socklen_t length = temp->ai_addrlen;
    struct sockaddr *address = temp->ai_addr;

    while (!is_shutdown)
    {
        snprintf(buffer, buffer_length, "%lu", counter++);

        number_bytes = sendto(socket_fd, buffer, buffer_length, 0, address, length);

   //...

我在这做了什么?我错过了什么?我不确定这里发生了什么。

1 个答案:

答案 0 :(得分:-1)

Frankie_C指出他的[评论]:

temp是指向struct addrinfo的指针,该值从server_info分配。但是当你freeaddrinfo(server_info)释放内存并且temp指向无处时。它第一次工作,因为还没有发生内存重用。但是在内存重用之后它变得无效。在释放结构之前尝试复制数据。