Mercurial > code
view modules/net/examples/blocking-accept.cpp @ 524:554d130f45ee
Net: resurrection
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 01 Jun 2016 17:11:45 +0200 |
parents | 0002b8da93dc |
children |
line wrap: on
line source
/* * blocking-accept.cpp -- example of blocking accept * * Options: * - WITH_PORT (int), the port to use (default: 16000) * - WITH_TIMEOUT (int), number of seconds before giving up (default: 60) * - WITH_SSL (bool), true to test with SSL (default: false) */ #include <iostream> #include "sockets.h" #if !defined(WITH_PORT) # define WITH_PORT 16000 #endif #if !defined(WITH_TIMEOUT) # define WITH_TIMEOUT 60 #endif int main() { #if defined(WITH_SSL) net::SocketTls<net::address::Ip> master; net::SocketTls<net::address::Ip> client{net::Invalid}; #else net::SocketTcp<net::address::Ip> master; net::SocketTcp<net::address::Ip> client{net::Invalid}; #endif net::Listener<> listener; try { master.set(net::option::SockReuseAddress{true}); master.bind(net::address::Ip{"*", WITH_PORT}); master.listen(); listener.set(master.handle(), net::Condition::Readable); listener.wait(std::chrono::seconds(WITH_TIMEOUT)); master.accept(client); } catch (const net::Error &error) { std::cerr << "error: " << error.what() << std::endl; std::exit(1); } std::cout << "Client successfully accepted!" << std::endl; return 0; }