#include #include #include #include #include #include "TcpSocket.hpp" TcpSocket::TcpSocket() : sock(-1), addr() { if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) perror("TcpSocket: Could not create socket"); } bool TcpSocket::bind(short port) { addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(port); if (::bind(sock, reinterpret_cast(&addr), sizeof(addr))) { perror("TcpSocket: Could not bind address to socket"); return false; } if (::listen(sock, 1)) { perror("TcpSocket: Could not listen for connections"); return false; } return true; } bool TcpSocket::listen(TcpSocket& client) { socklen_t len = sizeof(client.addr); client.sock = accept(sock, reinterpret_cast(&client.addr), &len); return client.sock != -1; } void TcpSocket::recv(std::string& out, size_t len) { std::vector buffer(len); int ret = read(sock, buffer.data(), len); if (ret != -1) out = { buffer.begin(), buffer.begin()+ret }; else out = ""; } size_t TcpSocket::send(const std::string& str) { int ret = write(sock, str.c_str(), str.length()); return ret != -1 ? ret : 0; } void TcpSocket::disconnect() { close(sock); sock = -1; } std::string TcpSocket::getRemoteAddress() const { char buffer[INET_ADDRSTRLEN+1] = { }; return inet_ntop(AF_INET, &addr, buffer, INET_ADDRSTRLEN+1); } short TcpSocket::getRemotePort() const { return ntohs(addr.sin_port); }