Socket programming: sendto always fails with errno 22 (EINVAL)

无人久伴 提交于 2019-11-29 10:51:47

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)

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);
chao wang

You have:

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!