#include "clientreader.h" ClientReader::ClientReader() { buf = new char[65536]; host_ip = new char[48]; strcpy(host_ip, "127.0.0.1"); } ClientReader::~ClientReader() { delete[] buf; delete[] host_ip; } int ClientReader::exec() { if (!createSocket()) { return -1; } sendMessage("reader"); std::cout << std::endl; std::string message; while ((message = receiveMessage()) != "") { std::cout << message << std::endl; } return 0; } bool ClientReader::createSocket() { sockd = socket(AF_INET, SOCK_STREAM, 0); if (sockd == -1) { std::cout << "Client: Error: cannot create socket!" << std::endl; return false; } struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); // Convert IPv4 and IPv6 addresses from text to binary form if(inet_pton(AF_INET, host_ip, &addr.sin_addr)<=0) { printf("\nInvalid address/Address not supported \n"); return -1; } if (connect(sockd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { printf("Client: Error: Connection Failed\n"); return false; } return true; } void ClientReader::sendMessage(std::string message) { send(sockd, message.c_str(), message.length(), 0); } std::string ClientReader::receiveMessage() { memset(buf, 0, 65536); recv(sockd, buf, 65536, 0); return std::string(buf); }