fix: finnaly working!

This commit is contained in:
AfonsoCMSousa 2026-01-07 22:40:22 +00:00
parent ba606c8619
commit ff07d6dcdf
3 changed files with 19 additions and 16 deletions

Binary file not shown.

View File

@ -16,7 +16,8 @@ using namespace std;
class Socket {
private:
// Socket file descriptor
int sock;
int sock_server;
int sock_unix;
struct sockaddr_in server_addr;
struct sockaddr_un server_addr_unix;

View File

@ -2,8 +2,9 @@
#include <sys/types.h>
Socket::Socket() {
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
sock_server = socket(AF_INET, SOCK_DGRAM, 0);
sock_unix = -1;
if (sock_server < 0) {
throw runtime_error("Failed to create socket");
}
memset(&server_addr, 0, sizeof(server_addr));
@ -11,7 +12,8 @@ Socket::Socket() {
}
Socket::~Socket() {
close(sock);
close(sock_server);
close(sock_unix);
}
void Socket::connect_server(const char *ip, uint16_t port) {
@ -23,51 +25,51 @@ void Socket::connect_server(const char *ip, uint16_t port) {
// Connect to existing UNIX domain socket (e.g., /tmp/socket_path)
void Socket::connect_unix(const char *ip, uint16_t port) {
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
sock_unix = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock_unix < 0) {
throw std::runtime_error("Failed to create UNIX socket");
}
this->server_addr_unix.sun_family = AF_UNIX;
strncpy(this->server_addr_unix.sun_path, ip, sizeof(this->server_addr_unix.sun_path) - 1);
printf("Connecting to UNIX socket at %s\n", ip);
printf("Connecting to UNIX socket at %s\n", ip);
if (connect(sock, (struct sockaddr *)&server_addr_unix, sizeof(server_addr_unix)) < 0) {
if (connect(sock_unix, (struct sockaddr *)&server_addr_unix, sizeof(server_addr_unix)) < 0) {
throw std::runtime_error("Failed to connect to UNIX socket");
}
}
void Socket::send_server() {
ssize_t sent_bytes = sendto(sock, &packet_data, sizeof(packet_data), 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
ssize_t sent_bytes = sendto(sock_server, &packet_data, sizeof(packet_data), 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (sent_bytes < 0) {
throw runtime_error("Failed to send data");
}
}
void Socket::send_unix() {
ssize_t sent_bytes = send(sock, &packet_data, sizeof(packet_data), 0);
ssize_t sent_bytes = send(sock_unix, &packet_data, sizeof(packet_data), 0);
if (sent_bytes < 0) {
throw runtime_error("Failed to send data to UNIX socket");
}
}
void Socket::send_server(const api_packet &data) {
ssize_t sent_bytes = sendto(sock, &data, sizeof(data), 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
ssize_t sent_bytes = sendto(sock_server, &data, sizeof(data), 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (sent_bytes < 0) {
throw runtime_error("Failed to send data");
}
}
void Socket::send_server(const void *data, size_t len) {
ssize_t sent_bytes = sendto(sock, data, len, 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (sent_bytes < 0) {
throw runtime_error("Failed to send data");
}
ssize_t sent_bytes = sendto(sock_server, data, len, 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (sent_bytes < 0) {
throw runtime_error("Failed to send data");
}
}
void Socket::send_unix(const api_packet &data) {
ssize_t sent_bytes = send(sock, &data, sizeof(data), 0);
ssize_t sent_bytes = send(sock_unix, &data, sizeof(data), 0);
if (sent_bytes < 0) {
throw runtime_error("Failed to send data to UNIX socket");
}