From a59eb494c73c3b7f676c3e3ef11e7c6d47b36d0a Mon Sep 17 00:00:00 2001 From: Friedrich Engel Date: Sat, 29 Jan 2022 16:20:50 +0100 Subject: [PATCH 1/2] added bind_broadcast for UDP_Server --- async-sockets/include/udpserver.hpp | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/async-sockets/include/udpserver.hpp b/async-sockets/include/udpserver.hpp index 06f4794..2704307 100644 --- a/async-sockets/include/udpserver.hpp +++ b/async-sockets/include/udpserver.hpp @@ -23,8 +23,39 @@ class UDPServer : public UDPSocket return; } } + void Bind(int port, FDR_ON_ERROR) { this->Bind("0.0.0.0", port, onError); } + + void Bind_Broadcast(std::string IPv4, std::uint16_t port, FDR_ON_ERROR) + { + if (inet_pton(AF_INET, IPv4.c_str(), &this->address.sin_addr) <= 0) + { + onError(errno, "Invalid address. Address type not supported."); + return; + } + + this->address.sin_family = AF_INET; + this->address.sin_port = htons(port); + + if (bind(this->sock, (const sockaddr*)&this->address, sizeof(this->address)) < 0) + { + onError(errno, "Cannot bind the socket."); + return; + } + + int broadcast = 1; + if (setsockopt(this->sock, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof broadcast)) + { + onError(errno, "setsockopt(SO_BROADCAST) failed."); + return; + } + } + + void Bind_Broadcast(std::uint16_t port, FDR_ON_ERROR) + { + this->Bind_Broadcast("0.0.0.0", port, onError); + } }; \ No newline at end of file From d61a1a98673fe3095e0fd3bd55883f9ec6266c93 Mon Sep 17 00:00:00 2001 From: Friedrich Engel Date: Sat, 29 Jan 2022 17:57:15 +0100 Subject: [PATCH 2/2] added bind_broadcast for UDP_Server --- async-sockets/include/udpserver.hpp | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/async-sockets/include/udpserver.hpp b/async-sockets/include/udpserver.hpp index 2704307..addccfc 100644 --- a/async-sockets/include/udpserver.hpp +++ b/async-sockets/include/udpserver.hpp @@ -29,23 +29,8 @@ class UDPServer : public UDPSocket this->Bind("0.0.0.0", port, onError); } - void Bind_Broadcast(std::string IPv4, std::uint16_t port, FDR_ON_ERROR) + void setBroadcast(FDR_ON_ERROR) { - if (inet_pton(AF_INET, IPv4.c_str(), &this->address.sin_addr) <= 0) - { - onError(errno, "Invalid address. Address type not supported."); - return; - } - - this->address.sin_family = AF_INET; - this->address.sin_port = htons(port); - - if (bind(this->sock, (const sockaddr*)&this->address, sizeof(this->address)) < 0) - { - onError(errno, "Cannot bind the socket."); - return; - } - int broadcast = 1; if (setsockopt(this->sock, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof broadcast)) { @@ -53,9 +38,4 @@ class UDPServer : public UDPSocket return; } } - - void Bind_Broadcast(std::uint16_t port, FDR_ON_ERROR) - { - this->Bind_Broadcast("0.0.0.0", port, onError); - } }; \ No newline at end of file