-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoardcastTest.m
157 lines (121 loc) · 4.03 KB
/
BoardcastTest.m
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// Created by Toni Möckel on 03.08.15.
//
#import "BoardcastTest.h"
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
@implementation BoardcastTest {
NSTimer *_connectionTimer;
}
- (void)broadCast
{
int socketSD = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (socketSD <= 0) {
NSLog(@"Error: Could not open socket.");
return;
}
// set socket options enable broadcast
int broadcastEnable = 1;
int ret = setsockopt(socketSD, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable));
if (ret) {
NSLog(@"Error: Could not open set socket to broadcast mode");
close(socketSD);
return;
}
// Configure the port and ip we want to send to
struct sockaddr_in broadcastAddr;
memset(&broadcastAddr, 0, sizeof(broadcastAddr));
broadcastAddr.sin_family = AF_INET;
inet_pton(AF_INET, "224.0.5.128", &broadcastAddr.sin_addr);
broadcastAddr.sin_port = htons(2362);
char strChar[14] = "";
strChar[0] = 'D';
strChar[1] = 'I';
strChar[2] = 'G';
strChar[3] = 'I';
strChar[4] = 0x00;
strChar[5] = 0x01;
strChar[6] = 0x00;
strChar[7] = 0x06;
strChar[8] = 0xff;
strChar[9] = 0xff;
strChar[10] = 0xff;
strChar[11] = 0xff;
strChar[12] = 0xff;
strChar[13] = 0xff;
ret = sendto(socketSD, strChar, strlen(strChar), 0, (struct sockaddr*)&broadcastAddr, sizeof(broadcastAddr));
if (ret < 0) {
NSLog(@"Error: Could not open send broadcast.");
close(socketSD);
return;
}
close(socketSD);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self listenForPackets];
});
[_connectionTimer invalidate];
_connectionTimer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(checkConnection) userInfo:nil repeats:NO];
}
- (void)checkConnection {
}
- (void)listenForPackets
{
int listeningSocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (listeningSocket <= 0) {
NSLog(@"Error: listenForPackets - socket() failed.");
return;
}
// set timeout to 2 seconds.
struct timeval timeV;
timeV.tv_sec = 2;
timeV.tv_usec = 0;
if (setsockopt(listeningSocket, SOL_SOCKET, SO_RCVTIMEO, &timeV, sizeof(timeV)) == -1) {
NSLog(@"Error: listenForPackets - setsockopt failed");
close(listeningSocket);
return;
}
// bind the port
struct sockaddr_in sockaddr;
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin_len = sizeof(sockaddr);
sockaddr.sin_family = AF_INET;
sockaddr.sin_port = htons(2362);
sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
int status = bind(listeningSocket, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
if (status == -1) {
close(listeningSocket);
NSLog(@"Error: listenForPackets - bind() failed.");
return;
}
// receive
struct sockaddr_in receiveSockaddr;
socklen_t receiveSockaddrLen = sizeof(receiveSockaddr);
size_t bufSize = 9216;
void *buf = malloc(bufSize);
ssize_t result = recvfrom(listeningSocket, buf, bufSize, 0, (struct sockaddr *)&receiveSockaddr, &receiveSockaddrLen);
NSData *data = nil;
if (result > 0) {
if ((size_t)result != bufSize) {
buf = realloc(buf, result);
}
data = [NSData dataWithBytesNoCopy:buf length:result freeWhenDone:YES];
char addrBuf[INET_ADDRSTRLEN];
if (inet_ntop(AF_INET, &receiveSockaddr.sin_addr, addrBuf, (size_t)sizeof(addrBuf)) == NULL) {
addrBuf[0] = '\0';
}
NSString *address = [NSString stringWithCString:addrBuf encoding:NSASCIIStringEncoding];
NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
[self didReceiveMessage:msg fromAddress:address];
});
} else {
free(buf);
}
close(listeningSocket);
}
- (void)didReceiveMessage:(NSString *)message fromAddress:(NSString *)address
{
}
@end