Created 'client' and 'server' classes, using easysock
This commit is contained in:
		
							
								
								
									
										59
									
								
								client.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								client.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,59 @@ | ||||
| #include "easysock.h" | ||||
| #include <iostream> | ||||
|  | ||||
| /* | ||||
| Client class - Defines a TCP/UDP client. | ||||
| - The constructor takes in a **remote** address and port, and connects the client to that address/port. | ||||
| */ | ||||
|  | ||||
| class Client { | ||||
| private: | ||||
| 	int ip_ver; | ||||
| 	char protocol; | ||||
| 	int port; | ||||
| 	int sock_fd; | ||||
| 	std::string address; | ||||
|  | ||||
| 	void create_socket() { | ||||
|                 struct sockaddr* dest = (struct sockaddr *)malloc(sizeof(struct sockaddr)); | ||||
|                 this->sock_fd = create_remote(this->ip_ver, this->protocol, this->address.data(), this->port, dest); | ||||
| 	} | ||||
|  | ||||
| public: | ||||
| 	Client(int ip_ver, char protocol, const char* address, int port) { | ||||
|                 /* Error checking */ | ||||
|                 if (ip_ver != 4 && ip_ver != 6) { | ||||
|                         throw std::invalid_argument("Invalid IP address type"); | ||||
|                 } | ||||
|                 if (port < 1024 || port > 65535) { | ||||
|                         throw std::invalid_argument("Invalid port"); | ||||
|                 } | ||||
|                 if (protocol != 'T' && protocol != 'U') { | ||||
|                         throw std::invalid_argument("Invalid protocol"); | ||||
|                 } | ||||
|  | ||||
|                 this->ip_ver = ip_ver; | ||||
|                 this->protocol =  protocol; | ||||
|                 this->port = port; | ||||
| 		this->address = std::string(address); | ||||
|                 create_socket(); | ||||
|         } | ||||
|  | ||||
| 	void sendAll(std::string to_send) { | ||||
| 		int str_length = to_send.length(); | ||||
| 		int num_bytes_sent = 0; /* Number of bytes sent in one call to send */ | ||||
| 		int total_bytes_sent = 0; /* Total number of bytes sent */ | ||||
|  | ||||
| 		while (total_bytes_sent < str_length) { | ||||
| 			num_bytes_sent = send(this->getSockFD(), to_send.substr(total_bytes_sent).data(), str_length - total_bytes_sent, 0); | ||||
| 			total_bytes_sent += num_bytes_sent; | ||||
| 		} | ||||
|  | ||||
| 		return; | ||||
| 	} | ||||
|  | ||||
| 	int getSockFD() { | ||||
| 		return sock_fd; | ||||
| 	} | ||||
|  | ||||
| }; | ||||
							
								
								
									
										62
									
								
								server.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								server.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,62 @@ | ||||
| #include "easysock.h" | ||||
| #include <iostream> | ||||
|  | ||||
| /* | ||||
| Server class - Defines a TCP/UDP server. | ||||
| - The constructor takes in a **local** address and port, on which the server listens. | ||||
| */ | ||||
|  | ||||
| class Server { | ||||
| private: | ||||
| 	int ip_ver; | ||||
| 	char protocol; | ||||
| 	int port; | ||||
| 	int sock_fd; | ||||
| 	std::string address; | ||||
|  | ||||
| 	void create_socket() { | ||||
| 		struct sockaddr* dest = (struct sockaddr *)malloc(sizeof(struct sockaddr)); | ||||
| 		this->sock_fd = create_local(this->ip_ver, this->protocol, this->address.data(), this->port, dest); | ||||
| 	} | ||||
|  | ||||
| public: | ||||
| 	Server(int ip_ver, char protocol, int port) { | ||||
| 		/* Error checking */ | ||||
| 		if (ip_ver != 4 && ip_ver != 6) { | ||||
| 			throw std::invalid_argument("Invalid IP address type"); | ||||
| 		} | ||||
| 		if (port < 1024 || port > 65535) { | ||||
| 			throw std::invalid_argument("Invalid port"); | ||||
| 		} | ||||
| 		if (protocol != 'T' && protocol != 'U') { | ||||
| 			throw std::invalid_argument("Invalid protocol"); | ||||
| 		} | ||||
|  | ||||
| 		this->ip_ver = ip_ver; | ||||
| 		this->protocol = protocol; | ||||
| 		this->port = port; | ||||
| 		if (ip_ver == 4) { | ||||
| 			this->address = "127.0.0.1"; | ||||
| 		} else if (ip_ver == 6) { | ||||
| 			this->address = "::1"; | ||||
| 		} | ||||
|  | ||||
| 		create_socket(); | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	std::string recvAll() { | ||||
| 		std::string string = std::string(); | ||||
| 		char* buffer = (char *)malloc(100); | ||||
| 		while (recv(this->getSockFD(), buffer, 100, 0) != 0) { | ||||
| 			string.append(std::string(buffer)); | ||||
| 		} | ||||
|  | ||||
| 		return string; | ||||
| 	} | ||||
|  | ||||
| 	int getSockFD() { | ||||
| 		return sock_fd; | ||||
| 	} | ||||
|  | ||||
| }; | ||||
		Reference in New Issue
	
	Block a user