working commit
This commit is contained in:
+11
-10
@@ -17,24 +17,25 @@ extern "C" {
|
||||
#include <resolver.hpp>
|
||||
#include <udpclient.hpp>
|
||||
|
||||
|
||||
UDPClient::UDPClient(void) {
|
||||
sockfd = 0;
|
||||
rmax = 2024;
|
||||
};
|
||||
|
||||
std::expected<void, std::string> UDPClient::Bind(std::string hostname, int nport) {
|
||||
std::expected<void, std::string> UDPClient::Bind(std::string naddress, int nport) {
|
||||
port = nport;
|
||||
address = naddress;
|
||||
|
||||
Resolver resolver;
|
||||
auto resolveRes = resolver.Resolve(hostname);
|
||||
if (!resolveRes) {
|
||||
auto msg = std::format("Cannot resolve hostname {}: {}", hostname, resolveRes.error());
|
||||
return std::unexpected(msg);
|
||||
struct sockaddr_in sa;
|
||||
if (inet_pton(AF_INET, address.c_str(), &(sa.sin_addr)) == 1) {
|
||||
family = AF_INET;
|
||||
} else if (inet_pton(AF_INET6, address.c_str(), &(sa.sin_addr)) == 1) {
|
||||
family = AF_INET6;
|
||||
} else {
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Incorrect address " + naddress);
|
||||
}
|
||||
auto addr = resolveRes.value();
|
||||
address = addr.GetAddress();
|
||||
family = addr.GetFamily();
|
||||
|
||||
if ((sockfd = socket(family, SOCK_DGRAM, 0)) < 0) {
|
||||
int errnocopy = errno;
|
||||
|
||||
Reference in New Issue
Block a user