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