为什么对 sendto() 的调用失败?
我尝试发送具有特定负载的单个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您未能为
getaddrinfo
指定service
参数。尝试这样调用getaddrinfo
:或者
You failed to specify a
service
parameter togetaddrinfo
. Try invokinggetaddrinfo
thus:or