#If OBJC
#import <netinet/in.h>
#import <sys/socket.h>
#import <unistd.h>
#import <arpa/inet.h>
- (void)sendBroadcastPacket {
// Open a socket
int sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sd<=0) {
NSLog(@"Error: Could not open socket");
return;
}
// Set socket options
// Enable broadcast
int broadcastEnable=1;
int ret=setsockopt(sd, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable));
if (ret) {
NSLog(@"Error: Could not open set socket to broadcast mode");
close(sd);
return;
}
// Since we don't call bind() here, the system decides on the port for us, which is what we want.
// Configure the port and ip we want to send to
struct sockaddr_in broadcastAddr; // Make an endpoint
memset(&broadcastAddr, 0, sizeof broadcastAddr);
broadcastAddr.sin_family = AF_INET;
inet_pton(AF_INET, "255.255.255.255", &broadcastAddr.sin_addr); // Set the broadcast IP address
broadcastAddr.sin_port = htons(3662); // Set port 3662
// Send the broadcast request, ie "Any upnp devices out there?"
char *request = "ARQFIND ROLLCALL:\x00";
ret = sendto(sd, request, strlen(request), 0, (struct sockaddr*)&broadcastAddr, sizeof broadcastAddr);
if (ret<0) {
NSLog(@"Error: Could not open send broadcast");
close(sd);
return;
}
close(sd);
}
#End If