diff options
author | cflip <cflip@cflip.net> | 2022-07-07 09:50:38 -0600 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2022-07-07 09:50:38 -0600 |
commit | 76e6c3d690caac3faa664b7b8b79cbc7c6a58394 (patch) | |
tree | 8b0ebbc6fe543a1dc0b9d8eba72efd35f8817aa6 /src/ServerConnection.cpp |
Initial commit
Diffstat (limited to 'src/ServerConnection.cpp')
-rw-r--r-- | src/ServerConnection.cpp | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/ServerConnection.cpp b/src/ServerConnection.cpp new file mode 100644 index 0000000..dd4fe86 --- /dev/null +++ b/src/ServerConnection.cpp @@ -0,0 +1,43 @@ +#include "ServerConnection.h" + +#include <sys/socket.h> +#include <sys/types.h> +#include <signal.h> +#include <unistd.h> +#include <arpa/inet.h> +#include <sys/time.h> +#include <sys/ioctl.h> +#include <netdb.h> + +#include "ClientConnection.h" + +static void error_and_die(const char* message) +{ + perror(message); + exit(1); +} + +ServerConnection::ServerConnection(int port) +{ + sockaddr_in address; + + if ((m_socket_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) + error_and_die("Failed to create socket"); + + bzero(&address, sizeof(address)); + address.sin_family = AF_INET; + address.sin_addr.s_addr = htonl(INADDR_ANY); + address.sin_port = htons(port); + + if ((bind(m_socket_fd, (sockaddr*)&address, sizeof(address))) < 0) + error_and_die("bind"); + + if ((listen(m_socket_fd, 10)) < 0) + error_and_die("listen"); +} + +ClientConnection ServerConnection::accept_client_connection() +{ + int client_socket = accept(m_socket_fd, (sockaddr*)nullptr, nullptr); + return ClientConnection(client_socket); +} |