Mercurial > code
view C++/examples/Socket/stream-client.cpp @ 485:898d8b29a4f1
Switch to lowercase filenames
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 12 Nov 2015 21:53:36 +0100 |
parents | 5a9671dabb15 |
children |
line wrap: on
line source
/* * stream-server -- example of stream server * * Options: * - WITH_PORT (int) which port to use (default: 12000) * - WITH_HOST (string literal) which host to connect (default: "localhost") * - WITH_SSL (bool) true to use SSL (default: false) */ #include <iostream> #include "Sockets.h" #if !defined(WITH_PORT) # define WITH_PORT 12000 #endif #if !defined(WITH_HOST) # define WITH_HOST "localhost" #endif #if defined(WITH_SSL) using Client = net::StreamClient<net::address::Ip, net::protocol::Tls>; #else using Client = net::StreamClient<net::address::Ip, net::protocol::Tcp>; #endif int main() { Client client; /* * Unfortunately at the moment the socket state is not changed, this will be done * in the future. */ bool connected{true}; client.setConnectionHandler([&] () { std::cout << "client: successfully connected" << std::endl; client.send("Hello world!"); }); client.setDisconnectionHandler([&] () { std::cout << "client: disconnected" << std::endl; connected = false; }); client.setErrorHandler([&] (const net::Error &error) { std::cout << "client: error: " << error.function() << ": " << error.what() << std::endl; connected = false; }); client.setReadHandler([] (const std::string &data) { std::cout << "client: received: " << data << std::endl; }); client.setWriteHandler([] (const std::string &data) { std::cout << "client: sent: " << data << std::endl; }); client.connect(net::address::Ip{WITH_HOST, WITH_PORT}); while (connected) { client.poll(); } std::cout << "client: exiting" << std::endl; return 0; }