套接字编程:sendto 总是失败,错误号 22 (EINVAL)

发布于 2024-09-28 18:19:02 字数 1219 浏览 2 评论 0原文

使用此代码,我总是没有收到任何字节发送,错误号为 22(EINVAL,无效参数)。 destination_host 在其他地方设置并且已知是有效的,所以我真的不明白发生了什么。 MAXMSGSIZE 为 1000。没有错误或警告。我正在使用 -Wall -Werror -pedantic 进行编译

char *data_rec;
u_int data_len;

int sockfd;
uint16_t *ns;
struct sockaddr_in address;
struct sockaddr *addr;

char *ip;

int i;
int errno;
int bytes_sent;

data_len = MAXMSGSIZE;
data_rec = malloc(sizeof(char)*MAXMSGSIZE);
ns = malloc(MAXMSGSIZE*sizeof(uint16_t));
ip = malloc(MAXMSGSIZE*sizeof(char));

data_rec = "some random test stuff";

sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(sockfd<0) {
    printf("socket() failed\n");
}

inet_ntop(AF_INET,destination_host->h_addr,ip,MAXMSGSIZE);
memset(&address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(theirPort);
address.sin_addr.s_addr = (unsigned long)ip;

addr = (struct sockaddr*)&address;
bind(sockfd,addr,sizeof(address));
/*Convert the message to uint16_t*/
for(i=0; i<MAXMSGSIZE; i++) {
    ns[i] = htons(data_rec[i]);
}


/* send the message */
bytes_sent = sendto(sockfd, ns, data_len, MSG_DONTWAIT, addr, sizeof(addr));
if(bytes_sent == -1) {
    printf("Error sending: %i\n",errno);
}

I am always getting no bytes sent, with an errno of 22 (EINVAL, Invalid Argument) with this code. The destination_host is set elsewhere and known to be valid, so I really don't see what is going on. MAXMSGSIZE is 1000. No errors, or warnings. I am compiling with -Wall -Werror -pedantic

char *data_rec;
u_int data_len;

int sockfd;
uint16_t *ns;
struct sockaddr_in address;
struct sockaddr *addr;

char *ip;

int i;
int errno;
int bytes_sent;

data_len = MAXMSGSIZE;
data_rec = malloc(sizeof(char)*MAXMSGSIZE);
ns = malloc(MAXMSGSIZE*sizeof(uint16_t));
ip = malloc(MAXMSGSIZE*sizeof(char));

data_rec = "some random test stuff";

sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(sockfd<0) {
    printf("socket() failed\n");
}

inet_ntop(AF_INET,destination_host->h_addr,ip,MAXMSGSIZE);
memset(&address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(theirPort);
address.sin_addr.s_addr = (unsigned long)ip;

addr = (struct sockaddr*)&address;
bind(sockfd,addr,sizeof(address));
/*Convert the message to uint16_t*/
for(i=0; i<MAXMSGSIZE; i++) {
    ns[i] = htons(data_rec[i]);
}


/* send the message */
bytes_sent = sendto(sockfd, ns, data_len, MSG_DONTWAIT, addr, sizeof(addr));
if(bytes_sent == -1) {
    printf("Error sending: %i\n",errno);
}

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

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

发布评论

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

评论(3

失去的东西太少 2024-10-05 18:19:02

您提供的地址大小错误。 addr 实际上是一个 struct sockaddr_in,而不是 struct sockaddr

将sendto的最后一个参数更改为sizeof(address)

You're giving the wrong size for the address. addr is really a struct sockaddr_in, not a struct sockaddr.

Change the last parameter of sendto to sizeof(address)

滥情稳全场 2024-10-05 18:19:02

inet_ntop 可能不是您想要的 - 它从网络(即有线)格式转换为演示格式(即“1.2.3.4”) 。尝试:

address.sin_addr.s_addr = *((unsigned long *)destination_host->h_addr);

inet_ntop probably isn't what you want - it converts from network (i.e. wire) format into presentation format (i.e. "1.2.3.4"). Try:

address.sin_addr.s_addr = *((unsigned long *)destination_host->h_addr);
玩心态 2024-10-05 18:19:02

您有:

bytes_sent = sendto(sockfd, ns, data_len, MSG_DONTWAIT, addr, sizeof(addr));

因为 sizeof(addr) == 4(或 8),所以使用 sizeof(*addr)

You have:

bytes_sent = sendto(sockfd, ns, data_len, MSG_DONTWAIT, addr, sizeof(addr));

Because sizeof(addr) == 4 (or 8), use sizeof(*addr).

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