Async  1.5.0
AsyncTcpClient_demo.cpp

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

#include <iostream>
#include <AsyncTcpClient.h>
using namespace std;
using namespace Async;
class MyClass : public sigc::trackable
{
public:
MyClass(void)
{
con = new TcpClient<>("www.linux.org", 80);
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();
}
~MyClass(void)
{
delete con;
}
private:
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)
{
MyClass my_class;
app.exec();
}
AsyncTcpClient.h
Contains a class for creating TCP client connections.
Async::TcpConnection
A class for handling exiting TCP connections.
Definition: AsyncTcpConnection.h:143
Async::CppApplication::exec
void exec(void)
Execute the application main loop.
Async::CppApplication
An application class for writing non GUI applications.
Definition: AsyncCppApplication.h:156
Async::TcpClient
A class for creating a TCP client connection.
Definition: AsyncTcpClient.h:151
AsyncCppApplication.h
The core class for writing asyncronous cpp applications.
Async
Namespace for the asynchronous programming classes.
Definition: AsyncApplication.h:75
Async::TcpConnection::write
virtual int write(const void *buf, int count)
Write data to the TCP connection.
Async::TcpConnection::remoteHost
const IpAddress & remoteHost(void) const
Return the IP-address of the remote host.
Definition: AsyncTcpConnection.h:237