Побайтовое чтение в функции chat()

This commit is contained in:
Alexander Zhirov 2022-09-06 17:21:06 +03:00
parent 550cf32e27
commit 1bcbee1c36
2 changed files with 13 additions and 20 deletions

View File

@ -39,7 +39,7 @@ class ServerTCP: public Server // @suppress("Class has a virtual method and non-
private: private:
bool _bind; bool _bind;
const unsigned short int _sizeBuffer; const unsigned short int _sizeBuffer;
std::unique_ptr<char[]> _buffer; std::unique_ptr<std::byte[]> _buffer;
class Hook class Hook
{ {
@ -54,6 +54,7 @@ private:
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 bind(); void bind();

View File

@ -8,12 +8,11 @@
#include <server.hpp> #include <server.hpp>
#include <iostream> #include <iostream>
#include <unistd.h> #include <unistd.h>
#include <string.h>
namespace zh namespace zh
{ {
#define MAX 1024
Server::Server(const unsigned short int port) : _port(port), _connfd(-1) Server::Server(const unsigned short int port) : _port(port), _connfd(-1)
{ {
_sizeClient = sizeof(*_client); _sizeClient = sizeof(*_client);
@ -48,7 +47,7 @@ ServerTCP::ServerTCP(const unsigned short int port, const unsigned short int siz
{ {
_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); _local = std::make_unique<Address>(AF_INET, INADDR_ANY, port);
_buffer = std::make_unique<char[]>(sizeBuffer); _buffer = std::make_unique<std::byte[]>(sizeBuffer);
} }
void ServerTCP::bind() void ServerTCP::bind()
@ -86,27 +85,20 @@ void ServerTCP::listen()
} }
} }
void ServerTCP::readData(const int sizeData)
{
}
void ServerTCP::chat() void ServerTCP::chat()
{ {
// char buff[MAX];
// int n;
while (true) while (true)
{ {
// bzero(buff, sizeof(buff));
// printf("Enter the string : ");
// n = 0;
// while ((buff[n++] = getchar()) != '\n')
// ;
// write(_connfd, buff, sizeof(buff));
bzero(_buffer.get(), _sizeBuffer); bzero(_buffer.get(), _sizeBuffer);
read(_connfd, _buffer.get(), _sizeBuffer); read(_connfd, _buffer.get(), 4);
std::cout << _buffer.get() << std::endl; std::cout << *reinterpret_cast<int*>(_buffer.get()) << std::endl;
// printf("From Server : %s", *_buffer); close(_connfd);
// if ((strncmp((*_buffer), "exit", 4)) == 0) break;
// {
// printf("Client Exit...\n");
// break;
// }
} }
} }