udp 局域网广播接受代码

/*发送广播*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> 
main()
{
 int fd=socket(AF_INET,SOCK_DGRAM,0);
 
 struct sockaddr_in addr;
 addr.sin_family=AF_INET;
 addr.sin_port=htons(11111);
 addr.sin_addr.s_addr=inet_addr("192.168.180.255"); 
 //设置socket选项为广播
 int iscast=1;
 setsockopt(fd,SOL_SOCKET,SO_BROADCAST,&iscast,sizeof(iscast));
 
 sendto(fd,"Killer",6,0,(struct sockaddr*)&addr,sizeof(addr));
 
 close(fd);
}

你可能感兴趣的:(socket,struct)