49 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			49 lines
		
	
	
		
			1.3 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);
 | 
						|
 | 
						|
	/* Method to receive data sent to the 'other_socket' socket */
 | 
						|
	std::string recvAll();
 | 
						|
 | 
						|
	/* 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
 |