Mercurial > code
view C++/examples/Socket/stream-client.cpp @ 463:214f03b47d4e
Socket:
- New action() and condition() function to check for pending events,
- More documentation,
- StreamServer, StreamClient can now complete recv/send operation for OpenSSL.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 04 Nov 2015 18:01:22 +0100 |
parents | |
children | 5a9671dabb15 |
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::Ipv4, net::Tls>; #else using Client = net::StreamClient<net::Ipv4, net::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::Ipv4{WITH_HOST, WITH_PORT}); while (connected) { client.poll(); } std::cout << "client: exiting" << std::endl; return 0; }