#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <cstring>
#include <cstdlib>
#include <iostream>
#define SOCKET_PATH "/tmp/hello_socket"
#define BUFFER_SIZE 64
int main() {
int sock_fd, client_fd;
struct sockaddr_un addr;
char buffer[BUFFER_SIZE];
if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, SOCKET_PATH, sizeof(addr.sun_path) - 1);
unlink(SOCKET_PATH);
if (bind(sock_fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
perror("bind");
exit(1);
}
if (listen(sock_fd, 5) == -1) {
perror("listen");
exit(1);
}
stdcout << "Server ready. Waiting for triggers..." << stdendl;
while (true) {
if ((client_fd = accept(sock_fd, nullptr, nullptr)) == -1) {
perror("accept");
continue;
}
ssize_t bytes = read(client_fd, buffer, BUFFER_SIZE);
if (bytes > 0) {
write(1, "Hello ACGO\n", 10);
}
close(client_fd);
}
close(sock_fd);
return 0;
}