tweaks to make work

This commit is contained in:
2026-07-23 22:41:47 -05:00
parent 86becee951
commit d091879773
2 changed files with 15 additions and 4 deletions

View File

@@ -1,6 +1,8 @@
#include "TcpClient.hpp"
#include <arpa/inet.h>
TcpClient::TcpClient(ConfigService* config, LoggerService* logger) : TcpBase(logger) {
if(!(config->getConfig<ClientConfig>("TcpClient", "main", &configuration_))) {
@@ -33,7 +35,7 @@ ErrorCode TcpClient::init() {
// parse the hostname string into uint8s
host = gethostbyname(configuration_.hostname.c_str());
if(host == NULL) {
logger_->log("TcpClient", LogFlag::Debug, "Unable to parse hostname.");
logger_->log("TcpClient", LogFlag::Error, "Unable to parse hostname.");
close(socketFd_); // TODO: probably on a destructor
return ErrorCode::Error;
}
@@ -41,7 +43,7 @@ ErrorCode TcpClient::init() {
// create tcp socket
socketFd_ = socket(AF_INET, SOCK_STREAM, 0);
if(socketFd_ < 0) {
logger_->log("TcpClient", LogFlag::Debug, "Unable to open client socket.");
logger_->log("TcpClient", LogFlag::Error, "Unable to open client socket.");
close(socketFd_);
return ErrorCode::Error;
}
@@ -52,9 +54,13 @@ ErrorCode TcpClient::init() {
serverAddress.sin_port = htons(configuration_.port);
serverAddress.sin_addr = *((struct in_addr*)host->h_addr);
char ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, host->h_addr_list[0], ip, sizeof(ip));
logger_->log("TcpClient", LogFlag::Error, "Connecting to {}:{}", ip, configuration_.port);
// connect to server
if(connect(socketFd_, (struct sockaddr*)&serverAddress, sizeof(struct sockaddr)) < 0) {
logger_->log("TcpClient", LogFlag::Debug, "Unable to connect to server.");
logger_->log("TcpClient", LogFlag::Error, "Unable to connect to server.");
close(socketFd_);
return ErrorCode::Error;
}