#ifndef _SOCK_CLASS #define _SOCK_CLASS #include #if defined(__unix__) || defined(__unix) ||(defined(__APPLE__) && defined(__MACH__)) #include #endif #ifdef _WIN32 #include #include #endif /* Global constants - can be used by children classes as return values, and by any clients to check what type the socket is of */ const int SOCK_CLIENT = 'C'; const int SOCK_SERVER = 'S'; /* Abstract class for a Socket. 'Client' and 'Server' derive from this class, and extend the functions defined here. */ class Sock { protected: int ip_ver; char protocol; int port; int sock_fd; std::string address; struct sockaddr_storage* dest; socklen_t addrlen; int other_socket; // The peer socket (the client if this socket is a server, and the server if this socket is a client) */ virtual void create_socket(); public: /* Default constructor */ Sock() {} /* Virtual destructor */ virtual ~Sock(); /* Regular constructor - defined in sock.cpp */ Sock(char protocol, const char* address, int port); /* Method to send data in 'to_send' through the 'other_socket' socket */ void sendAll(std::string to_send); /* Same as method above, with buffer and buffer size */ void sendAll(char* buffer, int size); /* Method to receive data sent to the 'other_socket' socket */ char* recvAll(); /* Non-blocking receive method - calls the method above after polling for data. Returns an empty string if there is nothing to read. */ char* recvAllNB(); /* Returns socket identifier */ int getSockFD(); /* This is a pure virtual function (AKA an abstract function). It's purpose is to be redefined by the children classes (client and server). */ virtual int get_type() = 0; }; #endif