当前位置: 技术问答>linux和unix
socket sendto: Network is unreachable?
来源: 互联网 发布时间:2015-04-23
本文导语: 以下程序发个广播出错: Network is unreachable 原因是什么? 怎么解决? #include #include #include #include main() { int fdUdp = socket(PF_INET, SOCK_DGRAM, 0); int opt = 1; int len = sizeof(opt); setsocko...
以下程序发个广播出错: Network is unreachable
原因是什么? 怎么解决?
#include
#include
#include
#include
main()
{
int fdUdp = socket(PF_INET, SOCK_DGRAM, 0);
int opt = 1;
int len = sizeof(opt);
setsockopt(fdUdp, SOL_SOCKET, SO_BROADCAST, (char *)&opt, len);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(12345);
bind(fdUdp, (struct sockaddr *)&sin, sizeof(struct sockaddr));
struct sockaddr_in addrBroadCast;
addrBroadCast.sin_family = AF_INET;
addrBroadCast.sin_addr.s_addr = htonl(INADDR_BROADCAST);
addrBroadCast.sin_port = htons(22345);
char aHello[] = "Hello world!";
int res = sendto(fdUdp, aHello, strlen(aHello), 0,
(struct sockaddr *)&addrBroadCast, sizeof(struct sockaddr));
if (res
原因是什么? 怎么解决?
#include
#include
#include
#include
main()
{
int fdUdp = socket(PF_INET, SOCK_DGRAM, 0);
int opt = 1;
int len = sizeof(opt);
setsockopt(fdUdp, SOL_SOCKET, SO_BROADCAST, (char *)&opt, len);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(12345);
bind(fdUdp, (struct sockaddr *)&sin, sizeof(struct sockaddr));
struct sockaddr_in addrBroadCast;
addrBroadCast.sin_family = AF_INET;
addrBroadCast.sin_addr.s_addr = htonl(INADDR_BROADCAST);
addrBroadCast.sin_port = htons(22345);
char aHello[] = "Hello world!";
int res = sendto(fdUdp, aHello, strlen(aHello), 0,
(struct sockaddr *)&addrBroadCast, sizeof(struct sockaddr));
if (res
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。