1. 服务端接收广播信号的程序
服务端receiver.c
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <signal.h>
#include <time.h>
#include <arpa/inet.h>
int sockfd;
void sig_handler(int signo)//信号处理函数
{
if(signo == SIGINT){
printf("receiver will exited\n");
close(sockfd);
exit(1);
}
}
int main(int argc, char *argv[])
{
if(argc < 2){
fprintf(stderr, "usage: %s port\n", argv[0]);
exit(1);
}
if(signal(SIGINT, sig_handler) == SIG_ERR){//登记一个sigint信号,以及对应的sig_handler信号处理函数
perror("signal sigint error");
exit(1);
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0);//创建socket,广播只能用在UDP中
if(sockfd < 0){
perror("socket error");
exit(1);
}
struct sockaddr_in serveraddr; //定义服务器地址结构体
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(atoi(argv[1]));
serveraddr.sin_addr.s_addr = INADDR_ANY;//绑定服务器上所有地址;也可以专门绑定广播地址,但要把点分十进制转换成网络字节序
if(bind(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) < 0){
perror("bind error");
exit(1);
}
char buffer[1024];
struct sockaddr_in clientaddr;//定义客户端地址结构体,并创建缓存,用于保存接收广播信息
socklen_t len = sizeof(clientaddr);
while(1){
memset(buffer, 0, sizeof(buffer));
memset(&clientaddr, 0, sizeof(clientaddr));
if(recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)&clientaddr, &len) < 0){
perror("recvfrom error");
exit(1);
}
else{
char ip[16];
inet_ntop(AF_INET, &clientaddr.sin_addr.s_addr, ip, sizeof(ip));
int port = ntohs(clientaddr.sin_port);
printf("%s(%d): %s\n", ip, port, buffer);
}
}
return 0;
}
2. 客户端发送广播信号的程序
客户端broadcast.c
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <signal.h>
#include <time.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
if(argc < 3){
fprintf(stderr, "usage: %s ip port\n", argv[0]);
exit(1);
}
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0){
perror("socket error");
exit(1);
}
int opt = 1;
//采用广播方式发送
setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(opt));
struct sockaddr_in serveraddr;
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(atoi(argv[2]));
inet_pton(AF_INET, argv[1], &serveraddr.sin_addr.s_addr);
printf("I will broadcast...\n");
char *info = "hello I'm client";
size_t size = strlen(info) * sizeof(char);
if(sendto(sockfd, info, size, 0, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) < 0){
perror("sendto error");
exit(1);
}
else{
printf("broadcast success\n");
}
close(sockfd);
return 0;
}
3. 运行结果
如果在同一个网段中,另一台机器上也打开一个receiver,使用同样的端口8888, 也会收到这个广播信息。