#include <AsyncTcpClient.h>
#include <iostream> #include <AsyncCppApplication.h> #include <AsyncTcpClient.h> using namespace std; using namespace Async; class MyClass : public SigC::Object { public: MyClass(void) { con = new TcpClient("www.linux.org", 80); con->connected.connect(slot(*this, &MyClass::onConnected)); con->disconnected.connect(slot(*this, &MyClass::onDisconnected)); con->dataReceived.connect(slot(*this, &MyClass::onDataReceived)); con->connect(); } ~MyClass(void) { delete con; } private: TcpClient *con; void onConnected(void) { cout << "Connection established to " << con->remoteHost() << "...\n"; con->write("GET /\n", 6); } void onDisconnected(TcpConnection *con, TcpClient::DisconnectReason reason) { cout << "Disconnected from " << con->remoteHost() << "...\n"; Application::app().quit(); } int onDataReceived(TcpConnection *con, void *buf, int count) { char *str = static_cast<char *>(buf); string html(str, str+count); cout << html; return count; } }; int main(int argc, char **argv) { CppApplication app; MyClass my_class; app.exec(); }
Definition at line 130 of file AsyncTcpClient.h.
Async::TcpClient::TcpClient | ( | const std::string & | remote_host, | |
uint16_t | remote_port, | |||
size_t | recv_buf_len = DEFAULT_RECV_BUF_LEN | |||
) |
Constructor.
remote_host | The hostname of the remote host | |
remote_port | The port on the remote host to connect to | |
recv_buf_len | The length of the receiver buffer to use |
Async::TcpClient::~TcpClient | ( | void | ) |
Destructor.
void Async::TcpClient::connect | ( | void | ) |
Connect to the remote host.
This function will initiate a connection to the remote host. The connection must not be written to before the connected signal (see TcpClient::connected) has been emitted. If the connection is already established or pending, nothing will be done.
void Async::TcpClient::disconnect | ( | void | ) |
Disconnect from the remote host.
Call this function to disconnect from the remote host. If already disconnected, nothing will be done. The disconnected signal is not emitted when this function is called
Reimplemented from Async::TcpConnection.
SigC::Signal0<void> Async::TcpClient::connected |
A signal that is emitted when a connection has been established.
Definition at line 173 of file AsyncTcpClient.h.