为什么对 sendto() 的调用失败?

发布于 2024-12-29 19:25:07 字数 1729 浏览 0 评论 0原文

我尝试发送具有特定负载的单个 UDP 数据包,但对 sendto() 的调用在运行时失败。这是我第一次尝试套接字编程。

原始代码是一个更大项目的一部分,但我已将其简化为一个更简单的测试程序:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>

#define SIZE 16

int main()
{
     int sock;
     struct sockaddr *addr;
     socklen_t addrlen;
     struct addrinfo hints, *result;
     char *url = "tracker.openbittorrent.com";
     void *payload = malloc(SIZE);

     memset(&hints, 0, sizeof(struct addrinfo));
     hints.ai_family = AF_INET;
     hints.ai_socktype = SOCK_DGRAM;
     hints.ai_flags = 0;
     hints.ai_protocol = IPPROTO_UDP;
     hints.ai_canonname = NULL;
     hints.ai_addr = NULL;
     hints.ai_next = NULL;

     int er = getaddrinfo(url, NULL, &hints, &result);
     if (er != 0) {
          fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(er));
          exit(EXIT_FAILURE);
     }

     sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
     if (sock == -1) {
          perror("socket");
          exit(EXIT_FAILURE);
     }

     if (sendto(sock, payload, SIZE, 0, result->ai_addr, result->ai_addrlen) == -1)
          perror("sendto");
 }

这是输出: sendto: Invalid argument

根据要求,一些 gdb 输出:

(gdb) p result->ai_addr->sa_data 
 $3 = "\000\000_\327>\005\000\000\000\000\000\000\000"
(gdb) p result->ai_addr->sa_family
 $4 = 2
(gdb) p result->ai_addrlen
 $5 = 16
(gdb) p result->ai_canonname
 $6 = 0x0
(gdb) p result->ai_flags
 $8 = 0
(gdb) p result->ai_protocol
 $9 = 17
(gdb) p result->ai_socktype
 $10 = 2

I'm trying to send a single UDP packet with a specific payload and my call to sendto() is failing at runtime. This is my first experiment with socket programming.

The original code is part of a larger project, but I've reduced it to a much simpler test program:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>

#define SIZE 16

int main()
{
     int sock;
     struct sockaddr *addr;
     socklen_t addrlen;
     struct addrinfo hints, *result;
     char *url = "tracker.openbittorrent.com";
     void *payload = malloc(SIZE);

     memset(&hints, 0, sizeof(struct addrinfo));
     hints.ai_family = AF_INET;
     hints.ai_socktype = SOCK_DGRAM;
     hints.ai_flags = 0;
     hints.ai_protocol = IPPROTO_UDP;
     hints.ai_canonname = NULL;
     hints.ai_addr = NULL;
     hints.ai_next = NULL;

     int er = getaddrinfo(url, NULL, &hints, &result);
     if (er != 0) {
          fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(er));
          exit(EXIT_FAILURE);
     }

     sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
     if (sock == -1) {
          perror("socket");
          exit(EXIT_FAILURE);
     }

     if (sendto(sock, payload, SIZE, 0, result->ai_addr, result->ai_addrlen) == -1)
          perror("sendto");
 }

Here's the output: sendto: Invalid argument

Some gdb output, as requested:

(gdb) p result->ai_addr->sa_data 
 $3 = "\000\000_\327>\005\000\000\000\000\000\000\000"
(gdb) p result->ai_addr->sa_family
 $4 = 2
(gdb) p result->ai_addrlen
 $5 = 16
(gdb) p result->ai_canonname
 $6 = 0x0
(gdb) p result->ai_flags
 $8 = 0
(gdb) p result->ai_protocol
 $9 = 17
(gdb) p result->ai_socktype
 $10 = 2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小…楫夜泊 2025-01-05 19:25:07

您未能为 getaddrinfo 指定 service 参数。尝试这样调用getaddrinfo

int er = getaddrinfo(url, "9", &hints, &result);

或者

int er = getaddrinfo(url, "discard", &hints, &result);

You failed to specify a service parameter to getaddrinfo. Try invoking getaddrinfo thus:

int er = getaddrinfo(url, "9", &hints, &result);

or

int er = getaddrinfo(url, "discard", &hints, &result);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文