fix отправки/чтения данных
This commit is contained in:
parent
4a5943ce9f
commit
09446a63f7
|
@ -22,7 +22,7 @@ public:
|
||||||
const unsigned short int getSize() const;
|
const unsigned short int getSize() const;
|
||||||
const std::string getString() const;
|
const std::string getString() const;
|
||||||
void clear();
|
void clear();
|
||||||
operator void*();
|
operator char*();
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,6 @@ class ClientTCP: public Server // @suppress("Class has a virtual method and non-
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
bool _connect;
|
bool _connect;
|
||||||
int _connfd;
|
|
||||||
|
|
||||||
void chat();
|
void chat();
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -75,7 +75,7 @@ int main(int argc, char *argv[])
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string host = optionHost.getValues()[0];
|
auto host = optionHost.getValues()[0];
|
||||||
|
|
||||||
client(port, host);
|
client(port, host);
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,9 +32,9 @@ void Buffer::clear()
|
||||||
bzero(_buffer.get(), _size);
|
bzero(_buffer.get(), _size);
|
||||||
}
|
}
|
||||||
|
|
||||||
Buffer::operator void*()
|
Buffer::operator char*()
|
||||||
{
|
{
|
||||||
return _buffer.get();
|
return reinterpret_cast<char*>(_buffer.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
namespace zh
|
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)
|
ClientTCP::ClientTCP(const std::string &address, const unsigned short int port, const unsigned short int sizeBuffer) : Server(port, sizeBuffer), _connect(false)
|
||||||
{
|
{
|
||||||
_socket = std::make_unique<Socket>(AF_INET, SOCK_STREAM, IPPROTO_IP);
|
_socket = std::make_unique<Socket>(AF_INET, SOCK_STREAM, IPPROTO_IP);
|
||||||
_address = std::make_unique<Address>(AF_INET, address, port);
|
_address = std::make_unique<Address>(AF_INET, address, port);
|
||||||
|
@ -27,7 +27,7 @@ void ClientTCP::connect()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (::connect((*_socket), reinterpret_cast<const sockaddr*>(&(*_address)), (*_address).size()) != 0)
|
if (::connect(*_socket, reinterpret_cast<const sockaddr*>(&(*_address)), (*_address).size()) != 0)
|
||||||
std::cerr << "Не удаётся подключить клиентский сокет к серверному сокету!" << std::endl;
|
std::cerr << "Не удаётся подключить клиентский сокет к серверному сокету!" << std::endl;
|
||||||
else
|
else
|
||||||
_connect = true;
|
_connect = true;
|
||||||
|
@ -37,12 +37,20 @@ void ClientTCP::connect()
|
||||||
|
|
||||||
void ClientTCP::chat()
|
void ClientTCP::chat()
|
||||||
{
|
{
|
||||||
|
Buffer *bf = &(*_buffer);
|
||||||
while (_connect)
|
while (_connect)
|
||||||
{
|
{
|
||||||
Buffer *bf = &(*_buffer);
|
|
||||||
bf->clear();
|
bf->clear();
|
||||||
std::cin.getline(reinterpret_cast<char*>(bf->operator void *()), bf->getSize());
|
std::cin.getline(*bf, bf->getSize());
|
||||||
write(_connfd, *bf, bf->getSize());
|
write(*_socket, *bf, bf->getSize());
|
||||||
|
bf->clear();
|
||||||
|
if (read(*_socket, *bf, bf->getSize()) > 0)
|
||||||
|
{
|
||||||
|
if (bf->getString() == "exit")
|
||||||
|
break;
|
||||||
|
else
|
||||||
|
std::cout << '\t' << bf->getString() << std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
close(*_socket);
|
close(*_socket);
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,8 +26,7 @@ void ServerTCP::Hook::execute(std::string buffer, ServerTCP &s)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
auto pos = buffer.find_first_of(_command);
|
if (buffer == _command)
|
||||||
if (pos != std::string::npos && pos == 0)
|
|
||||||
{
|
{
|
||||||
_handler(buffer, s);
|
_handler(buffer, s);
|
||||||
}
|
}
|
||||||
|
@ -54,7 +53,7 @@ void ServerTCP::bind()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (::bind((*_socket), reinterpret_cast<const sockaddr*>(&(*_address)), (*_address).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;
|
||||||
|
@ -85,16 +84,22 @@ void ServerTCP::listen()
|
||||||
|
|
||||||
void ServerTCP::chat()
|
void ServerTCP::chat()
|
||||||
{
|
{
|
||||||
|
Buffer *bf = &(*_buffer);
|
||||||
while (_listenLoop)
|
while (_listenLoop)
|
||||||
{
|
{
|
||||||
Buffer *bf = &(*_buffer);
|
|
||||||
bf->clear();
|
bf->clear();
|
||||||
int size = read(_connfd, *bf, bf->getSize());
|
int size = read(_connfd, *bf, bf->getSize());
|
||||||
if (size > 0)
|
if (size > 0)
|
||||||
|
{
|
||||||
std::for_each(_hooks.begin(), _hooks.end(), [&](Hook &hook)
|
std::for_each(_hooks.begin(), _hooks.end(), [&](Hook &hook)
|
||||||
{
|
{
|
||||||
hook.execute(bf->getString(), *this);
|
hook.execute(bf->getString(), *this);
|
||||||
});
|
});
|
||||||
|
if (!_listenLoop)
|
||||||
|
write(_connfd, "exit", 4);
|
||||||
|
else
|
||||||
|
write(_connfd, "OK", 2);
|
||||||
|
}
|
||||||
else if (size < 0)
|
else if (size < 0)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue