update socket

This commit is contained in:
PolynomialDivision 2018-03-12 16:11:24 +01:00
parent 5c50512362
commit 3d843ed19f

View file

@ -8,20 +8,17 @@
#include "networksocket.h" #include "networksocket.h"
#include "broadcastsocket.h" #include "broadcastsocket.h"
// TODO: Consider to remove this...
int setup_broadcast_socket(const char *_broadcast_ip, unsigned short _broadcast_port, struct sockaddr_in *addr) { int setup_broadcast_socket(const char *_broadcast_ip, unsigned short _broadcast_port, struct sockaddr_in *addr) {
int sock; int sock;
int broadcast_permission; int broadcast_permission;
/* Create socket */ // Create socket
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
fprintf(stderr, "Failed to create socket.\n"); fprintf(stderr, "Failed to create socket.\n");
return -1; return -1;
} }
/* Allow broadcast */ // Allow broadcast
broadcast_permission = 1; broadcast_permission = 1;
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void *) &broadcast_permission, if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void *) &broadcast_permission,
sizeof(broadcast_permission)) < 0) { sizeof(broadcast_permission)) < 0) {
@ -29,15 +26,14 @@ int setup_broadcast_socket(const char *_broadcast_ip, unsigned short _broadcast_
return -1; return -1;
} }
/* Construct Address */ // Constract addess
memset(addr, 0, sizeof(*addr)); memset(addr, 0, sizeof(*addr));
addr->sin_family = AF_INET; addr->sin_family = AF_INET;
addr->sin_addr.s_addr = inet_addr(_broadcast_ip); addr->sin_addr.s_addr = inet_addr(_broadcast_ip);
addr->sin_port = htons(_broadcast_port); addr->sin_port = htons(_broadcast_port);
/* Bind socket */ // Bind socket
while (bind(sock, (struct sockaddr *) addr, sizeof(*addr)) < while (bind(sock, (struct sockaddr *) addr, sizeof(*addr)) < 0) {
0) {
fprintf(stderr, "Binding socket failed!\n"); fprintf(stderr, "Binding socket failed!\n");
sleep(1); sleep(1);
} }