本文整理汇总了C++中tcp::acceptor类的典型用法代码示例。如果您正苦于以下问题:C++ acceptor类的具体用法?C++ acceptor怎么用?C++ acceptor使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了acceptor类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: thread
peer_server()
: m_peer_requests(0)
, m_acceptor(m_ios)
, m_port(0)
{
error_code ec;
m_acceptor.open(tcp::v4(), ec);
if (ec)
{
fprintf(stderr, "Error opening peer listen socket: %s\n", ec.message().c_str());
return;
}
m_acceptor.bind(tcp::endpoint(address_v4::any(), 0), ec);
if (ec)
{
fprintf(stderr, "Error binding peer socket to port 0: %s\n", ec.message().c_str());
return;
}
m_port = m_acceptor.local_endpoint(ec).port();
if (ec)
{
fprintf(stderr, "Error getting local endpoint of peer socket: %s\n", ec.message().c_str());
return;
}
m_acceptor.listen(10, ec);
if (ec)
{
fprintf(stderr, "Error listening on peer socket: %s\n", ec.message().c_str());
return;
}
fprintf(stderr, "%s: peer initialized on port %d\n", time_now_string(), m_port);
m_thread.reset(new thread(boost::bind(&peer_server::thread_fun, this)));
}
示例2: getClientConnection
void getClientConnection (tcp::acceptor &acceptor) {
std::cout << "In GUIMessageSenderThread getClientConnection()" << std::endl;
try
{
std::cout << "waiting for gui client to connect..." << std::endl;
acceptor.accept(*stream_.rdbuf());
std::cout << "...gui client connected" << std::endl;
connected_ = true;
}
catch (std::exception& e)
{
std::cerr << "Error in gui client connection: " << e.what() << std::endl;
}
std::cout << "done GUIMessageSenderThread getClientConnection()" << std::endl;
}
示例3: client
/*****************************************************************************
* fiber function per client
*****************************************************************************/
void client( boost::asio::io_service & io_svc, tcp::acceptor & a,
boost::fibers::barrier& barrier, unsigned iterations) {
print( tag(), ": echo-client started");
for (unsigned count = 0; count < iterations; ++count) {
tcp::resolver resolver( io_svc);
tcp::resolver::query query( tcp::v4(), "127.0.0.1", "9999");
tcp::resolver::iterator iterator = resolver.resolve( query);
tcp::socket s( io_svc);
boost::asio::connect( s, iterator);
for (unsigned msg = 0; msg < 1; ++msg) {
std::ostringstream msgbuf;
msgbuf << "from " << fiber_names.lookup() << " " << count << "." << msg;
std::string message(msgbuf.str());
print( tag(), ": Sending: ", message);
boost::system::error_code ec;
boost::asio::async_write(
s,
boost::asio::buffer( message),
boost::fibers::asio::yield[ec]);
if ( ec == boost::asio::error::eof) {
return; //connection closed cleanly by peer
} else if ( ec) {
throw boost::system::system_error( ec); //some other error
}
char reply[max_length];
size_t reply_length = s.async_read_some(
boost::asio::buffer( reply, max_length),
boost::fibers::asio::yield[ec]);
if ( ec == boost::asio::error::eof) {
return; //connection closed cleanly by peer
} else if ( ec) {
throw boost::system::system_error( ec); //some other error
}
print( tag(), ": Reply : ", std::string( reply, reply_length));
}
}
// done with all iterations, wait for rest of client fibers
if ( barrier.wait()) {
// exactly one barrier.wait() call returns true
// we're the lucky one
a.close();
print( tag(), ": acceptor stopped");
}
print( tag(), ": echo-client stopped");
}
示例4: acceptorLoop
void acceptorLoop()
{
cout << "Waiting for clients..." << endl;
for(;;)
{
socket_ptr clientSock(new tcp::socket(service));
acceptor.accept(*clientSock);
cout << "New client joined! ";
mtx.lock();
clientList->emplace_back(clientSock);
mtx.unlock();
cout << clientList->size() << " total clients" << endl;
}
}
示例5: server
/*****************************************************************************
* listening server
*****************************************************************************/
void server( boost::asio::io_service & io_svc, tcp::acceptor & a) {
print( tag(), ": echo-server started");
try {
for (;;) {
socket_ptr socket( new tcp::socket( io_svc) );
boost::system::error_code ec;
a.async_accept(
* socket,
boost::fibers::asio::yield[ec]);
if ( ec) {
throw boost::system::system_error( ec); //some other error
} else {
boost::fibers::fiber( session, socket).detach();
}
}
} catch ( std::exception const& ex) {
print( tag(), ": caught exception : ", ex.what());
}
io_svc.stop();
print( tag(), ": echo-server stopped");
}
示例6: run
void run(unsigned sleepMillis)
{
std::cout << "in GUIMessageSenderThread: run "
<< sleepMillis << "ms"
<< std::endl;
while (running_) {
if (!connected_) {
std::cout << "In GUIMessageSenderThread trying to connect to: " << host_ << " " << port_ << std::endl;
//tcp::endpoint endpoint_(boost::asio::ip::address::from_string(host_), port_);
//tcp::endpoint endpoint_(boost::asio::ip::address::from_string(host_), port_);
endpoint_.address(boost::asio::ip::address::from_string(host_));
endpoint_.port(port_);
tcp::acceptor acceptor(*io_service_, endpoint_);
getClientConnection(acceptor);
try {
sendMessages(sleepMillis);
acceptor.close();
stream_.clear();
stream_.close();
}
catch (std::exception& e)
{
std::cerr << "GUIMessageSenderThread Error in connection: " << e.what() << std::endl;
}
connected_ = false;
//boost::asio::deadline_timer timer(*io_service_,boost::posix_time::milliseconds(1000));
//timer.expires_from_now(boost::posix_time::milliseconds(1000));
//timer.wait();
}
}
std::cout << "done GUIMessageSenderThread: run "
<< sleepMillis << "ms"
<< std::endl;
}
示例7: accept
void accept()
{
auto self = shared_from_this();
awaitingConnection = std::make_shared<Connection>(io);
acceptor.async_accept(awaitingConnection->stream1(),
[this, self] (const error_code &error) {
if (error) {
std::cerr << "accept: " << error << std::endl;
} else {
Connection::Ptr thisConnecion;
std::swap(thisConnecion, awaitingConnection);
accept();
error_code localSocketError;
thisConnecion->stream2().connect(socketPath, localSocketError);
if (localSocketError) {
std::cerr << "connecting local socket: " << localSocketError << std::endl;
thisConnecion->stream1().close(localSocketError);
return;
}
thisConnecion->start();
}
});
}
示例8:
~peer_server()
{
m_acceptor.cancel();
m_acceptor.close();
if (m_thread) m_thread->join();
}
示例9: start
void TcpServer::start()
{
TcpConnection::SP_TCPCONNECTION spConnection = TcpConnection::create(m_acceptor.get_io_service());
m_acceptor.async_accept(spConnection->socket(),
boost::bind(&TcpServer::OnAccept, this, spConnection, boost::asio::placeholders::error));
}
示例10:
~CServer()
{
DEBUG_MSG("Listener Destroyed");
acceptor_.close();
}
示例11: start_accept
void start_accept()
{
shared_ptr<session> s(new session(acceptor_.io_service(),cache,sessions));
acceptor_.async_accept(s->socket_,boost::bind(&tcp_cache_server::on_accept,this,aio::placeholders::error,s));
}