Урок 3. Сетевые архитектуры. TCP-сокеты #3

Open
alexander wants to merge 5 commits from lesson_03 into master
2 changed files with 13 additions and 20 deletions
Showing only changes of commit 1bcbee1c36 - Show all commits

View File

@ -39,7 +39,7 @@ class ServerTCP: public Server // @suppress("Class has a virtual method and non-
private:
bool _bind;
const unsigned short int _sizeBuffer;
std::unique_ptr<char[]> _buffer;
std::unique_ptr<std::byte[]> _buffer;
class Hook
{
@ -54,6 +54,7 @@ private:
std::vector<Hook> _hooks;
void chat();
void readData(const int sizeData);
public:
ServerTCP(const unsigned short int port, const unsigned short int sizeBuffer = 1024);
void bind();

View File

@ -8,12 +8,11 @@
#include <server.hpp>
#include <iostream>
#include <unistd.h>
#include <string.h>
namespace zh
{
#define MAX 1024
Server::Server(const unsigned short int port) : _port(port), _connfd(-1)
{
_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);
_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()
@ -86,27 +85,20 @@ void ServerTCP::listen()
}
}
void ServerTCP::readData(const int sizeData)
{
}
void ServerTCP::chat()
{
// char buff[MAX];
// int n;
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);
read(_connfd, _buffer.get(), _sizeBuffer);
std::cout << _buffer.get() << std::endl;
// printf("From Server : %s", *_buffer);
// if ((strncmp((*_buffer), "exit", 4)) == 0)
// {
// printf("Client Exit...\n");
// break;
// }
read(_connfd, _buffer.get(), 4);
std::cout << *reinterpret_cast<int*>(_buffer.get()) << std::endl;
close(_connfd);
break;
}
}