You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.5 KiB
C++

#ifndef _SOCK_CLASS
#define _SOCK_CLASS
#include <string>
#include <sys/socket.h>
/* 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* 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() {}
/* Regular constructor - defined in sock.cpp */
Sock(int ip_ver, 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 */
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