# Pragma once
# Include <boost/asio. hpp>
# Include <boost/bind. hpp>
# Include <boost/shared_ptr.hpp>
Using boost: asio: ip: tcp;
Class CAsioClient
{
Public:
CAsioClient (boost: asio: io_service & io_service, tcp: endpoint & endpoint)
: Socket (io_service)
{
Socket. async_connect (endpoint,
Boost: bind (& CAsioClient: handle_connect, this, boost: asio: placeholders: error)
);
Memset (getBuffer, 0, sizeof (getBuffer ));
}
Virtual ~ CAsioClient (void)
{
If (sendBuffer! = NULL)
{
Delete sendBuffer;
}
}
Private:
Void handle_connect (const boost: system: error_code & error)
{
If (! Error)
{
SendBuffer = new BYTE [2];
SendBuffer [0] = 0x68;
SendBuffer [1] = 0x16;
Boost: asio: async_write (socket, boost: asio: buffer (sendBuffer, sizeof (sendBuffer )),
Boost: bind (& CAsioClient: handle_write, this, boost: asio: placeholders: error ));
Socket. async_read_some (boost: asio: buffer (getBuffer, sizeof (getBuffer )),
Boost: bind (& CAsioClient: handle_read, this, boost: asio: placeholders: error)
);
}
Else
{
Socket. close ();
}
}
Void handle_read (const boost: system: error_code & error)
{
If (! Error)
{
Socket. async_read_some (
Boost: asio: buffer (getBuffer, sizeof (getBuffer )),
Boost: bind (& CAsioClient: handle_read, this, boost: asio: placeholders: error)
);
}
Else
{
Socket. close ();
}
}
Void handle_write (const boost: system: error_code & error)
{
If (! Error)
{
Sleep (1000 );
SendBuffer = new BYTE [2];
SendBuffer [0] = 0x68;
SendBuffer [1] = 0x16;
Boost: asio: async_write (socket, boost: asio: buffer (sendBuffer, sizeof (sendBuffer )),
Boost: bind (& CAsioClient: handle_write, this, boost: asio: placeholders: error ));
}
}
Private:
Tcp: socket;
BYTE * sendBuffer;
BYTE getBuffer [1024];
};