Class TcpSocket

Class Documentation

class TcpSocket

TCP socket class.

This class contains everything needed for creating a TCP/IP socket, for clients and servers, reading and writing, etc.

Public Functions

TcpSocket()

basic class constructor

TcpSocket(uint16_t port_in, bool block_in, int totime_in, int id_in)

useful constructor for a server

TcpSocket(uint16_t port_in, bool block_in, bool async_in, int totime_in, int id_in)

useful constructor for a server

TcpSocket(std::string host, uint16_t port_in, bool block_in, bool async_in, int totime_in, int id_in)

useful constructor for a server

TcpSocket(std::string host, uint16_t port)

client constructor

TcpSocket(const TcpSocket &obj)

copy constructor

TcpSocket &operator=(const TcpSocket &obj)

copy assignment (explicit: atomic fd deletes the implicit one)

inline void set_totime(int time_in)
inline int getfd()
inline bool isblocking()
inline bool isasync()
inline bool isconnected()
inline int polltimeout()
inline void sethost(std::string host_in)
inline std::string gethost()
inline void setport(uint16_t port_in)
inline int getport()
int Accept()

creates a new connected socket for pending connection

int Listen()

create a TCP listening socket

int Poll()

polls a single file descriptor to wait for incoming data to read

int Poll(int timeout)

polls a single file descriptor with specified timeout

int Connect()

connect to this->host on this->port

int Close()

close a socket connection

ssize_t Read(void *buf, const size_t count)

read data from connected socket

ssize_t Read(std::string &retstring)

read data from connected socket until newline

ssize_t Read(std::string &retstring, const char &term)

read data from connected socket until terminating char found

ssize_t Read(std::string &retstring, const std::string &endstr)

read data from connected socket until endstr

int Bytes_ready()

get the number of bytes available on the socket descriptor this->fd

void Flush()

flush a socket by reading until it’s empty

ssize_t Write(std::string msg_in)

write data to a socket

ssize_t Write(const char *c)
template<class T>
inline ssize_t Write(T *buf, size_t count)

Public Members

struct addrinfo *addrs

dynamically allocated linked list returned by getaddrinfo()

int id

id may be useful for tracking multiple threads, no real requirement here