comparison 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
comparison
equal deleted inserted replaced
462:9d53c536372e 463:214f03b47d4e
1 /*
2 * stream-server -- example of stream server
3 *
4 * Options:
5 * - WITH_PORT (int) which port to use (default: 12000)
6 * - WITH_HOST (string literal) which host to connect (default: "localhost")
7 * - WITH_SSL (bool) true to use SSL (default: false)
8 */
9
10 #include <iostream>
11
12 #include "Sockets.h"
13
14 #if !defined(WITH_PORT)
15 # define WITH_PORT 12000
16 #endif
17
18 #if !defined(WITH_HOST)
19 # define WITH_HOST "localhost"
20 #endif
21
22 #if defined(WITH_SSL)
23 using Client = net::StreamClient<net::Ipv4, net::Tls>;
24 #else
25 using Client = net::StreamClient<net::Ipv4, net::Tcp>;
26 #endif
27
28 int main()
29 {
30 Client client;
31
32 /*
33 * Unfortunately at the moment the socket state is not changed, this will be done
34 * in the future.
35 */
36 bool connected{true};
37
38 client.setConnectionHandler([&] () {
39 std::cout << "client: successfully connected" << std::endl;
40 client.send("Hello world!");
41 });
42 client.setDisconnectionHandler([&] () {
43 std::cout << "client: disconnected" << std::endl;
44 connected = false;
45 });
46 client.setErrorHandler([&] (const net::Error &error) {
47 std::cout << "client: error: " << error.function() << ": " << error.what() << std::endl;
48 connected = false;
49 });
50 client.setReadHandler([] (const std::string &data) {
51 std::cout << "client: received: " << data << std::endl;
52 });
53 client.setWriteHandler([] (const std::string &data) {
54 std::cout << "client: sent: " << data << std::endl;
55 });
56
57 client.connect(net::Ipv4{WITH_HOST, WITH_PORT});
58
59 while (connected) {
60 client.poll();
61 }
62
63 std::cout << "client: exiting" << std::endl;
64
65 return 0;
66 }