当前位置: 技术问答>linux和unix
怎样编程发送广播包?我的这段程序发送怎么返回值总是-1?
来源: 互联网 发布时间:2015-05-08
本文导语: int main(int argc, char *argv[]) { int s,iSend; bool bBroadcast; char *sMsg="This is a test"; struct sockaddr_in bcast; s=socket(AF_INET, SOCK_RAW, IPPROTO_UDP); bBroadcast...
int main(int argc, char *argv[])
{
int s,iSend;
bool bBroadcast;
char *sMsg="This is a test";
struct sockaddr_in bcast;
s=socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
bBroadcast=true;
setsockopt(s,SOL_SOCKET,SO_BROADCAST,(char *)&bBroadcast,sizeof(bool));
bcast.sin_family=AF_INET;
bcast.sin_addr.s_addr=0xffffffff;
bcast.sin_port=htons(5150);
iSend=sendto(s,sMsg,strlen(sMsg),0,(struct sockaddr *)&bcast,sizeof(bcast));
}
为什么iSend总是等于-1?怎么发送广播包啊? 请各位老大帮忙啊!!
{
int s,iSend;
bool bBroadcast;
char *sMsg="This is a test";
struct sockaddr_in bcast;
s=socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
bBroadcast=true;
setsockopt(s,SOL_SOCKET,SO_BROADCAST,(char *)&bBroadcast,sizeof(bool));
bcast.sin_family=AF_INET;
bcast.sin_addr.s_addr=0xffffffff;
bcast.sin_port=htons(5150);
iSend=sendto(s,sMsg,strlen(sMsg),0,(struct sockaddr *)&bcast,sizeof(bcast));
}
为什么iSend总是等于-1?怎么发送广播包啊? 请各位老大帮忙啊!!
|
我改了一下,你自己看吧。
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int s,iSend;
int bBroadcast;
char *sMsg="This is a test";
struct sockaddr_in bcast;
s=socket(AF_INET, SOCK_DGRAM, 0);
bBroadcast=1;
setsockopt(s,SOL_SOCKET,SO_BROADCAST,(char *)&bBroadcast,sizeof(bBroadca
st));
bcast.sin_family=AF_INET;
bcast.sin_addr.s_addr=0xffffffff;
bcast.sin_port=htons(5150);
iSend=sendto(s,sMsg,strlen(sMsg),0,(struct sockaddr *)&bcast,sizeof(bcast));
printf("send:%dn",iSend);
}
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int s,iSend;
int bBroadcast;
char *sMsg="This is a test";
struct sockaddr_in bcast;
s=socket(AF_INET, SOCK_DGRAM, 0);
bBroadcast=1;
setsockopt(s,SOL_SOCKET,SO_BROADCAST,(char *)&bBroadcast,sizeof(bBroadca
st));
bcast.sin_family=AF_INET;
bcast.sin_addr.s_addr=0xffffffff;
bcast.sin_port=htons(5150);
iSend=sendto(s,sMsg,strlen(sMsg),0,(struct sockaddr *)&bcast,sizeof(bcast));
printf("send:%dn",iSend);
}