/*
* lingdulebaishi@163.com
* 23-Mar-2012 Auther.Zhang 0.2 Version
* Copyright & the right of find interpretation auther
* synopsis:UDP广播,在linux下gcc编译与网络调试助手调试成功
*
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 50000
#define MAXDATASIZE 34
int main(int argc,char *argv[])
{
int socket_fd;
struct sockaddr_in my_addr;
char buf[MAXDATASIZE];
int so_broadcast=1;
int i;
memset(buf,0,34);
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(PORT);
my_addr.sin_addr.s_addr=inet_addr("255.255.255.255");
bzero(&(my_addr.sin_zero),8);
if((socket_fd=(socket(AF_INET,SOCK_DGRAM,0)))==-1)
{
perror("socket");
exit(1);
}
setsockopt(socket_fd,SOL_SOCKET,SO_BROADCAST,&so_broadcast,sizeof(so_broadcast));
if(-1 == (bind(socket_fd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))))
{
perror("bind");
exit(1);
}
for(i=0; i<8; i++)
{
buf[i]=9;
}
for(i=0; i<10; i++)
{
sendto(socket_fd,buf,sizeof(buf),0,(struct sockaddr*)&my_addr,sizeof(my_addr));
}
return 0;
}
阅读(1643) | 评论(0) | 转发(0) |