Async 1.8.0
AsyncTcpClient_demo.cpp

An example of how to use the Async::TcpClient class

#include <iostream>
#include <sstream>
#include <AsyncTcpClient.h>
using namespace Async;
class MyClass : public sigc::trackable
{
public:
MyClass(std::string hostname, uint16_t port)
{
con.connected.connect(mem_fun(*this, &MyClass::onConnected));
con.disconnected.connect(mem_fun(*this, &MyClass::onDisconnected));
con.dataReceived.connect(mem_fun(*this, &MyClass::onDataReceived));
con.connect(hostname, port);
}
private:
void onConnected(void)
{
std::cout << "--- Connection established to " << con.remoteHost()
<< std::endl;
std::string req(
"GET / HTTP/1.0\r\n"
"Connection: Close\r\n"
"Host: " + con.remoteHostName() + "\r\n"
"\r\n");
std::cout << "--- Sending request:\n" << req << std::endl;
con.write(req.data(), req.size());
}
void onDisconnected(TcpConnection *, TcpClient<>::DisconnectReason reason)
{
std::cout << "--- Disconnected from " << con.remoteHost() << ": "
<< TcpConnection::disconnectReasonStr(reason)
<< std::endl;
Application::app().quit();
}
int onDataReceived(TcpConnection *, void *buf, int count)
{
std::cout << "--- Data received:" << std::endl;
std::cout.write(static_cast<char*>(buf), count);
std::cout << std::endl;
return count;
}
};
int main(int argc, char **argv)
{
MyClass my_class("checkip.amazonaws.com", 80);
app.exec();
}
The core class for writing asyncronous cpp applications.
Contains a class for creating TCP client connections.
An application class for writing non GUI applications.
void exec(void)
Execute the application main loop.
std::string remoteHostName(void) const
Get the name of the remote host as given to connect()
A class for creating a TCP client connection.
A class for handling exiting TCP connections.
const IpAddress & remoteHost(void) const
Return the IP-address of the remote host.
DisconnectReason
Reason code for disconnects.
virtual int write(const void *buf, int count)
Write data to the TCP connection.
Namespace for the asynchronous programming classes.