Mercurial > code
comparison modules/net/examples/blocking-connect.cpp @ 512:0002b8da93dc
Net: rework a lot, going to stabilization soon
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 04 Apr 2016 17:34:01 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
511:5bab839611f2 | 512:0002b8da93dc |
---|---|
1 /* | |
2 * blocking-connect.cpp -- example of blocking connect | |
3 * | |
4 * Options: | |
5 * - WITH_HOST (string literal), the host to try (default: "malikania.fr") | |
6 * - WITH_PORT (int), the port to use (default: 80) | |
7 * - WITH_TIMEOUT (int), number of seconds before giving up (default: 30) | |
8 * - WITH_SSL (bool), true to test with SSL (default: false) | |
9 */ | |
10 | |
11 #include <iostream> | |
12 | |
13 #if !defined(WITH_HOST) | |
14 # define WITH_HOST "malikania.fr" | |
15 #endif | |
16 | |
17 #if !defined(WITH_PORT) | |
18 # define WITH_PORT 80 | |
19 #endif | |
20 | |
21 #if !defined(WITH_TIMEOUT) | |
22 # define WITH_TIMEOUT 30 | |
23 #endif | |
24 | |
25 #include "elapsed-timer.h" | |
26 #include "sockets.h" | |
27 | |
28 int main() | |
29 { | |
30 #if defined(WITH_SSL) | |
31 net::SocketTls<net::address::Ipv4> socket; | |
32 #else | |
33 net::SocketTcp<net::address::Ipv4> socket; | |
34 #endif | |
35 | |
36 try { | |
37 std::cout << "Trying to connect to " << WITH_HOST << ":" << WITH_PORT << std::endl; | |
38 socket.connect(net::address::Ip(WITH_HOST, WITH_PORT)); | |
39 } catch (const net::Error &error) { | |
40 std::cerr << "error: " << error.what() << std::endl; | |
41 std::exit(1); | |
42 } | |
43 | |
44 std::cout << "Successfully connected!" << std::endl; | |
45 | |
46 return 0; | |
47 } | |
48 |