сервер + клиент

This commit is contained in:
Alexander Zhirov 2022-09-07 15:43:34 +03:00
parent 1bcbee1c36
commit 402c3f865f
11 changed files with 297 additions and 47 deletions

View File

@ -9,6 +9,9 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <string.h> #include <string.h>
#include <string>
#include <memory>
#include <netdb.h>
namespace zh { namespace zh {
@ -16,11 +19,13 @@ class Address
{ {
private: private:
struct sockaddr_in _addr; struct sockaddr_in _addr;
std::unique_ptr<const struct hostent *> _host;
unsigned int _len; unsigned int _len;
public: public:
Address(); Address();
Address(const Address &address); Address(const Address &address);
Address(const unsigned short int family, const unsigned int addr, const unsigned short int port); Address(const unsigned short int family, const unsigned int addr, const unsigned short int port);
Address(const unsigned short int family, const std::string &addr, const unsigned short int port);
unsigned int& size(); unsigned int& size();
struct sockaddr_in& operator*(); struct sockaddr_in& operator*();
}; };

View File

@ -0,0 +1,28 @@
/*
* buffer.hpp
*
* Created on: 7 сент. 2022 г.
* Author: alexander
*/
#pragma once
#include <memory>
namespace zh
{
class Buffer
{
private:
const unsigned short int _size;
std::unique_ptr<std::byte[]> _buffer;
public:
Buffer(const unsigned short int size);
const unsigned short int getSize() const;
const std::string getString() const;
void clear();
operator void*();
};
}

View File

@ -0,0 +1,27 @@
/*
* client.hpp
*
* Created on: 7 сент. 2022 г.
* Author: alexander
*/
#pragma once
#include <stock.hpp>
namespace zh
{
class ClientTCP: public Server // @suppress("Class has a virtual method and non-virtual destructor")
{
private:
bool _connect;
int _connfd;
void chat();
public:
ClientTCP(const std::string &address, const unsigned short int port, const unsigned short int sizeBuffer = 1024);
void connect();
};
}

View File

@ -9,37 +9,25 @@
//#include <algorithm> //#include <algorithm>
#include <socket.hpp> #include <stock.hpp>
#include <address.hpp>
#include <memory>
#include <vector> #include <vector>
#include <string> #include <string>
namespace zh namespace zh
{ {
class Server // @suppress("Class has a virtual method and non-virtual destructor") class ServerTCP;
{
protected:
std::unique_ptr<Socket> _socket;
std::unique_ptr<Address> _local, _client;
unsigned int _sizeClient;
const unsigned short int _port;
int _connfd;
public:
Server(const unsigned short int port);
virtual void bind() = 0;
virtual void listen() = 0;
};
typedef void (*hook)(std::string buffer, Server &s); typedef void (*hook)(std::string buffer, ServerTCP &s);
class ServerTCP: public Server // @suppress("Class has a virtual method and non-virtual destructor") class ServerTCP: public Server // @suppress("Class has a virtual method and non-virtual destructor")
{ {
private: private:
bool _bind; bool _bind;
const unsigned short int _sizeBuffer; int _connfd;
std::unique_ptr<std::byte[]> _buffer; bool _listenLoop;
unsigned int _sizeClient;
std::unique_ptr<Address> _client;
class Hook class Hook
{ {
@ -48,18 +36,19 @@ private:
hook _handler; hook _handler;
public: public:
Hook(std::string command, hook handler); Hook(std::string command, hook handler);
void execute(std::string buffer, Server &s); void execute(std::string buffer, ServerTCP &s);
}; };
std::vector<Hook> _hooks; std::vector<Hook> _hooks;
void chat(); void chat();
void readData(const int sizeData);
public: public:
ServerTCP(const unsigned short int port, const unsigned short int sizeBuffer = 1024); ServerTCP(const unsigned short int port, const unsigned short int sizeBuffer = 1024);
void registerHook(std::string command, hook handler);
void bind(); void bind();
void listen(); void listen();
void registerHook(std::string command, hook handler); void disconnect();
void stop();
}; };
} }

View File

@ -0,0 +1,31 @@
/*
* stock.hpp
*
* Created on: 7 сент. 2022 г.
* Author: alexander
*/
#pragma once
#include <memory>
#include <socket.hpp>
#include <address.hpp>
#include <buffer.hpp>
namespace zh
{
class Server // @suppress("Class has a virtual method and non-virtual destructor")
{
protected:
std::unique_ptr<Socket> _socket;
std::unique_ptr<Address> _address;
std::unique_ptr<Buffer> _buffer;
const unsigned short int _port;
public:
Server(const unsigned short int port, const unsigned short int sizeBuffer);
virtual void bind() {}
virtual void listen() {}
};
}

View File

@ -9,10 +9,44 @@
#include <iostream> #include <iostream>
#include <server.hpp> #include <server.hpp>
#include <client.hpp>
void print(std::string buffer, zh::ServerTCP &s)
{
std::cout << buffer << std::endl;
}
void stop(std::string buffer, zh::ServerTCP &s)
{
s.stop();
}
void disconnect(std::string buffer, zh::ServerTCP &s)
{
s.disconnect();
}
void server(const int port)
{
zh::ServerTCP tcpServer(port);
tcpServer.registerHook("", print);
tcpServer.registerHook("disconnect", disconnect);
tcpServer.registerHook("stop", stop);
tcpServer.bind();
tcpServer.listen();
}
void client(const int port, const std::string &host)
{
zh::ClientTCP tcpClient(host, port);
tcpClient.connect();
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
ap::Hub hub({{ "port", 'p', ap::REQUIRED }}); ap::Hub hub({{ "port", 'p', ap::REQUIRED }, { "host", 'h', ap::REQUIRED }});
hub.readArguments(argc, argv); hub.readArguments(argc, argv);
auto optionPort = hub.getOption('p'); auto optionPort = hub.getOption('p');
@ -21,12 +55,18 @@ int main(int argc, char *argv[])
std::cerr << "Порт не был установлен!" << std::endl; std::cerr << "Порт не был установлен!" << std::endl;
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
auto optionHost = hub.getOption('h');
if (!optionHost.isSet())
{
std::cerr << "Адрес не был установлен!" << std::endl;
exit(EXIT_FAILURE);
}
int port = std::stoi(optionPort.getValues()[0]); int port = std::stoi(optionPort.getValues()[0]);
std::string host = optionHost.getValues()[0];
zh::ServerTCP tcpServer(port); server(port);
tcpServer.bind(); // client(port, host);
tcpServer.listen();
return 0; return 0;
} }

View File

@ -31,6 +31,18 @@ Address::Address(const unsigned short int family, const unsigned int addr, const
_addr.sin_port = htons(port); _addr.sin_port = htons(port);
} }
Address::Address(const unsigned short int family, const std::string &addr, const unsigned short int port)
{
memset(&_addr, 0, sizeof(_addr));
_len = sizeof(_addr);
_host = std::make_unique<const struct hostent *>(gethostbyname(addr.c_str()));
_addr.sin_family = family;
_addr.sin_addr.s_addr = *reinterpret_cast<const in_addr_t*>((*_host)->h_addr);
_addr.sin_port = htons(port);
}
unsigned int& Address::size() unsigned int& Address::size()
{ {
return _len; return _len;

View File

@ -0,0 +1,40 @@
/*
* buffer.cpp
*
* Created on: 7 сент. 2022 г.
* Author: alexander
*/
#include <buffer.hpp>
#include <string.h>
#include <string>
namespace zh
{
Buffer::Buffer(const unsigned short int size) : _size(size)
{
_buffer = std::make_unique<std::byte[]>(_size);
}
const unsigned short int Buffer::getSize() const
{
return _size;
}
const std::string Buffer::getString() const
{
return reinterpret_cast<char*>(_buffer.get());
}
void Buffer::clear()
{
bzero(_buffer.get(), _size);
}
Buffer::operator void*()
{
return _buffer.get();
}
}

View File

@ -0,0 +1,50 @@
/*
* client.cpp
*
* Created on: 7 сент. 2022 г.
* Author: alexander
*/
#include <client.hpp>
#include <iostream>
#include <arpa/inet.h>
#include <unistd.h>
namespace zh
{
ClientTCP::ClientTCP(const std::string &address, const unsigned short int port, const unsigned short int sizeBuffer) : Server(port, sizeBuffer), _connect(false), _connfd(-1)
{
_socket = std::make_unique<Socket>(AF_INET, SOCK_STREAM, IPPROTO_IP);
_address = std::make_unique<Address>(AF_INET, address, port);
}
void ClientTCP::connect()
{
if (_connect)
{
std::cerr << "Клиентский сокет уже подключён к серверному сокету!" << std::endl;
return;
}
if (::connect((*_socket), reinterpret_cast<const sockaddr*>(&(*_address)), (*_address).size()) != 0)
std::cerr << "Не удаётся подключить клиентский сокет к серверному сокету!" << std::endl;
else
_connect = true;
chat();
}
void ClientTCP::chat()
{
while (_connect)
{
Buffer *bf = &(*_buffer);
bf->clear();
std::cin.getline(reinterpret_cast<char*>(bf->operator void *()), bf->getSize());
write(_connfd, *bf, bf->getSize());
}
close(*_socket);
}
}

View File

@ -9,20 +9,16 @@
#include <iostream> #include <iostream>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <algorithm>
namespace zh namespace zh
{ {
Server::Server(const unsigned short int port) : _port(port), _connfd(-1)
{
_sizeClient = sizeof(*_client);
}
ServerTCP::Hook::Hook(std::string command, hook handler) : _command(command), _handler(handler) ServerTCP::Hook::Hook(std::string command, hook handler) : _command(command), _handler(handler)
{ {
} }
void ServerTCP::Hook::execute(std::string buffer, Server &s) void ServerTCP::Hook::execute(std::string buffer, ServerTCP &s)
{ {
if (_command == "") if (_command == "")
{ {
@ -43,11 +39,11 @@ void ServerTCP::registerHook(std::string command, hook handler)
_hooks.push_back({ command, handler }); _hooks.push_back({ command, handler });
} }
ServerTCP::ServerTCP(const unsigned short int port, const unsigned short int sizeBuffer) : Server(port), _bind(false), _sizeBuffer(sizeBuffer) ServerTCP::ServerTCP(const unsigned short int port, const unsigned short int sizeBuffer) : Server(port, sizeBuffer), _bind(false), _connfd(-1), _listenLoop(true)
{ {
_socket = std::make_unique<Socket>(AF_INET, SOCK_STREAM, IPPROTO_IP); _socket = std::make_unique<Socket>(AF_INET, SOCK_STREAM, IPPROTO_IP);
_local = std::make_unique<Address>(AF_INET, INADDR_ANY, port); _address = std::make_unique<Address>(AF_INET, INADDR_ANY, port);
_buffer = std::make_unique<std::byte[]>(sizeBuffer); _sizeClient = sizeof(_client);
} }
void ServerTCP::bind() void ServerTCP::bind()
@ -58,7 +54,7 @@ void ServerTCP::bind()
return; return;
} }
if (::bind((*_socket), reinterpret_cast<const sockaddr*>(&(*_local)), (*_local).size()) != 0) if (::bind((*_socket), reinterpret_cast<const sockaddr*>(&(*_address)), (*_address).size()) != 0)
std::cerr << "Не удаётся связать адрес с дескриптором слушающего сокета!" << std::endl; std::cerr << "Не удаётся связать адрес с дескриптором слушающего сокета!" << std::endl;
else else
_bind = true; _bind = true;
@ -79,27 +75,41 @@ void ServerTCP::listen()
std::cerr << "Не удаётся верифицировать и принять пакеты от клиента!" << std::endl; std::cerr << "Не удаётся верифицировать и принять пакеты от клиента!" << std::endl;
return; return;
} }
else
_listenLoop = true;
// Обработка соединения с клиентом // Обработка соединения с клиентом
chat(); chat();
} }
} }
void ServerTCP::readData(const int sizeData)
{
}
void ServerTCP::chat() void ServerTCP::chat()
{ {
while (true) while (_listenLoop)
{ {
bzero(_buffer.get(), _sizeBuffer); Buffer *bf = &(*_buffer);
read(_connfd, _buffer.get(), 4); bf->clear();
std::cout << *reinterpret_cast<int*>(_buffer.get()) << std::endl; int size = read(_connfd, *bf, bf->getSize());
close(_connfd); if (size > 0)
break; std::for_each(_hooks.begin(), _hooks.end(), [&](Hook &hook)
{
hook.execute(bf->getString(), *this);
});
else if (size < 0)
break;
} }
close(_connfd);
}
void ServerTCP::stop()
{
_listenLoop = false;
_bind = 0;
}
void ServerTCP::disconnect()
{
_listenLoop = false;
} }
} }

View File

@ -0,0 +1,18 @@
/*
* stock.cpp
*
* Created on: 7 сент. 2022 г.
* Author: alexander
*/
#include <stock.hpp>
namespace zh
{
Server::Server(const unsigned short int port, const unsigned short int sizeBuffer) : _port(port)
{
_buffer = std::make_unique<Buffer>(sizeBuffer);
}
}