套接字sendto()返回EINVAL

时间:2014-04-10 02:56:55

标签: c sockets udp ipv4 sendto

我正在尝试在C中发送UDP数据包。我有以下sendto()

char* msg = "Hello";

//ret is the return value of getaddrinfo, the address is AF_INET (IPv4)
//and the sock_type is SOCK_DGRAM (UDP)
struct sockaddr_in *ip = (struct sockaddr_in *)ret->ai_addr;

if ((sendto(sock, msg, strlen(msg), 0, (struct sockaddr *)ip, 
                sizeof(struct sockaddr *))) != -1) {
    printf("msg sent successfully");
} else {
    printf("Error sending msg: %s\n", strerror(errno));
}

然而,它返回一个错误,说有一个无效的参数。查看联机帮助页,我无法确定哪一个是无效的参数。有什么想法吗?

编辑:这是我的所有代码

#include <stdio.h>  
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>

int main(int argc, const char *argv[])
{
    /*
     * Help the technically challenged among us who have no idea
     * what on God's green Earth they are doing with this thing.
    */
    if (argc != 2) {
        printf("usage: routetracer <ip address or hostname>\n");
        return -1;
    }

    /*
     * hints- parameters for return value of getaddrinfo
     * ret- return value of getaddrinfo
     */
    struct addrinfo hints, *ret;
    int status;
    char ipv4[INET_ADDRSTRLEN];
    int ttl = 0;
    char* msg = "Hello";
    int last_hop = 0;

    //define what we want from getaddrinfo
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_INET; //IPv4
    hints.ai_socktype = SOCK_DGRAM; //UDP packets

    //call getaddrinfo to fill ret, w/ error chk
    if ((status = getaddrinfo(argv[1], NULL, &hints, &ret)) != 0) {
        printf("getaddrinfo: %s\n", gai_strerror(status));
        return -1;
    }

    //extract IPv4 address from ret
    struct sockaddr_in* ip = (struct sockaddr_in *)ret->ai_addr;

    //convert address from pure numbers to something easier to read
    inet_ntop(ret->ai_family, &(ip->sin_addr), ipv4, INET_ADDRSTRLEN);   

    //kindly inform the user of which hostname they are connecting to
    printf("Route for: %s\n", ipv4);

    //create a socket
    int sock = socket(ret->ai_family, ret->ai_socktype, ret->ai_protocol);

    ttl = 1;
    if ((setsockopt(sock, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl))) != -1) {
        printf("TTL set successfully\n");
    } else {
        printf("Error setting TTL: %s\n", strerror(errno));
    }

    if ((sendto(sock, msg, strlen(msg), 0, ret->ai_addr,
                    ret->ai_addrlen)) != -1) {
        printf("msg sent successfully");
    } else {
      printf("Error sending msg: %s\n", strerror(errno));
    }
    return 0;
}

运行程序会得到以下输出:

$ ./routetracer www.google.com
Route for: 173.194.46.82
TTL set successfully
Error sending msg: Invalid argument

2 个答案:

答案 0 :(得分:4)

尝试:

if ((sendto(sock, msg, strlen(msg), 0, (struct sockaddr *)ip, 
                sizeof(struct sockaddr_in))) != -1) {

你给它一个指针的大小,而不是结构的大小。它需要是特定的结构类型,而不是泛型类型。

答案 1 :(得分:3)

正如Barmar指出的那样,EINVAL一个原因是错误的:

  sizeof(struct sockaddr *)

,它给出了指针的大小。请参阅Socket programming: sendto always fails with errno 22 (EINVAL)

第二个原因似乎是sin_portgetaddrinfo返回0。将其更改为80说清除EINVAL,如:

  ((struct sockaddr_in *)ret->ai_addr)->sin_port = htons(80); // test

这里的端口80不是HTTP,而是(对于UDP)是Google的实验性QUIC Chromium。

Wikipedia http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers声明端口0用于UDP 保留 ,并且TCP非正式保留为“用于指定系统分配的编程技术(动态) )港口“。

另外(并提到原始问题),您可能不需要担心变量ip。您正在将ret->ai_addr投射到struct sockaddr_in *,然后将再投放到其原始类型

而且,正如Remy Lebeau所指出的,最好使用service的{​​{1}}参数。所以把这些放在一起,你的代码看起来更像是:

getaddrinfo