view 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
line wrap: on
line source

/*
 * blocking-connect.cpp -- example of blocking connect
 *
 * Options:
 *   - WITH_HOST (string literal), the host to try (default: "malikania.fr")
 *   - WITH_PORT (int), the port to use (default: 80)
 *   - WITH_TIMEOUT (int), number of seconds before giving up (default: 30)
 *   - WITH_SSL (bool), true to test with SSL (default: false)
 */

#include <iostream>

#if !defined(WITH_HOST)
#  define WITH_HOST "malikania.fr"
#endif

#if !defined(WITH_PORT)
#  define WITH_PORT 80
#endif

#if !defined(WITH_TIMEOUT)
#  define WITH_TIMEOUT 30
#endif

#include "elapsed-timer.h"
#include "sockets.h"

int main()
{
#if defined(WITH_SSL)
	net::SocketTls<net::address::Ipv4> socket;
#else
	net::SocketTcp<net::address::Ipv4> socket;
#endif

	try {
		std::cout << "Trying to connect to " << WITH_HOST << ":" << WITH_PORT << std::endl;
		socket.connect(net::address::Ip(WITH_HOST, WITH_PORT));
	} catch (const net::Error &error) {
		std::cerr << "error: " << error.what() << std::endl;
		std::exit(1);
	}

	std::cout << "Successfully connected!" << std::endl;

	return 0;
}