diff options
| author | hovertank3d <[email protected]> | 2025-11-13 23:02:56 +0100 |
|---|---|---|
| committer | hovertank3d <[email protected]> | 2025-11-13 23:06:37 +0100 |
| commit | a5b377745116de2b269cdfc5b90f08f46d0432d2 (patch) | |
| tree | 7aea9482dadc4d66df17907282f2359c54c9df0d /include/net/client.hpp | |
| download | ogurec-a5b377745116de2b269cdfc5b90f08f46d0432d2.tar.gz ogurec-a5b377745116de2b269cdfc5b90f08f46d0432d2.zip | |
initial commit
Diffstat (limited to 'include/net/client.hpp')
| -rw-r--r-- | include/net/client.hpp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/include/net/client.hpp b/include/net/client.hpp new file mode 100644 index 0000000..8f81638 --- /dev/null +++ b/include/net/client.hpp @@ -0,0 +1,51 @@ +#pragma once + +#include "net/conn.hpp" +#include <cerrno> +#include <cstdio> +#include <cstdlib> +#include <cstring> +#include <stdexcept> +#include <string> + +#include <arpa/inet.h> +#include <fcntl.h> +#include <netdb.h> +#include <netinet/in.h> +#include <sys/socket.h> +#include <sys/time.h> +#include <sys/types.h> +#include <sys/uio.h> +#include <sys/wait.h> +#include <unistd.h> + +namespace net { + +class client { + struct sockaddr_in server_address {}; + +public: + client(std::string hostname, int port) + { + server_address.sin_family = AF_INET; + inet_pton(AF_INET, hostname.c_str(), &server_address.sin_addr); + server_address.sin_port = htons(port); + } + + conn connect() + { + int sock_fd; + + sock_fd = socket(AF_INET, SOCK_STREAM, 0); + if (sock_fd < 0) + throw std::runtime_error(strerror(errno)); + + auto err = ::connect(sock_fd, (struct sockaddr*)&server_address, + sizeof(server_address)); + if (err < 0) + throw std::runtime_error(strerror(errno)); + + return conn { sock_fd, server_address, sizeof(server_address) }; + } +}; +}; |
