套接字编程:sendto 总是失败,错误号 22 (EINVAL)
使用此代码,我总是没有收到任何字节发送,错误号为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您提供的地址大小错误。
addr
实际上是一个struct sockaddr_in
,而不是struct sockaddr
。将sendto的最后一个参数更改为
sizeof(address)
You're giving the wrong size for the address.
addr
is really astruct sockaddr_in
, not astruct sockaddr
.Change the last parameter of sendto to
sizeof(address)
inet_ntop
可能不是您想要的 - 它从网络(即有线)格式转换为演示格式(即“1.2.3.4”) 。尝试: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:您有:
因为
sizeof(addr) == 4
(或 8),所以使用sizeof(*addr)
。You have:
Because
sizeof(addr) == 4
(or 8), usesizeof(*addr)
.