-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbroadcast.c
88 lines (77 loc) · 2.12 KB
/
broadcast.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#define BROADCASTVREQUEST "DISCOVER_CEGATEWAY_REQUEST"
#define BROADCASTVRESPONSE "DISCOVER_CEGATEWAY_RESPONSE"
int main()
{
daemon(1,1);
setvbuf(stdout, NULL, _IONBF, 0);
fflush(stdout);
// 绑定地址
struct sockaddr addrto;
bzero(&addrto, sizeof(struct sockaddr));
((struct sockaddr_in *)&addrto)->sin_family = AF_INET;
((struct sockaddr_in *)&addrto)->sin_addr.s_addr = htonl(INADDR_ANY);
((struct sockaddr_in *)&addrto)->sin_port = htons(8888);
struct sockaddr from;
bzero(&from, sizeof(struct sockaddr));
// ((struct sockaddr_in *)&from)->sin_family = AF_INET;
// ((struct sockaddr_in *)&from)->sin_addr.s_addr = htonl(INADDR_ANY);
// ((struct sockaddr_in *)&from)->sin_port = htons(8888);
int sock = -1;
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
fprintf(stdout, "socket error\n");
return -1;
}
const int opt = 1;
//设置该套接字为广播类型,
int nb = 0;
nb = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&opt, sizeof(opt));
if(nb == -1)
{
fprintf(stdout, "set socket error...\n");
return -1;
}
if(bind(sock,(struct sockaddr *)&(addrto), sizeof(struct sockaddr)) == -1)
{
fprintf(stdout, "bind error...\n");
return -1;
}
int len = sizeof(struct sockaddr);
while(1)
{
//从广播地址接受消息
char smsg[128] = {0};
int ret=recvfrom(sock, smsg, 100, 0, (struct sockaddr*)&from,(socklen_t*)&len);
if(ret<=0)
{
fprintf(stdout, "read error....\n");
continue;
}
else
{
char *recip = inet_ntoa(((struct sockaddr_in *)&from)->sin_addr);
printf("from:%s>>>%s\n",recip, smsg);
if(0 == strcmp(BROADCASTVREQUEST, (const char *)smsg)){
if (sendto(sock, BROADCASTVRESPONSE, sizeof(BROADCASTVREQUEST), 0,
(struct sockaddr *) &from,
sizeof(struct sockaddr)) < 0) {
fprintf(stdout, "sendto error!\n");
continue;
}
}
//back res
fprintf(stdout, "send ok\n");
}
fprintf(stdout, "hhh\n");
}
return 0;
}