BOOST ASIO - 如何写控制台服务器 [英] BOOST ASIO - How to write console server
问题描述
我必须写异步TCP Sever的。
TCP服务器必须通过控制台进行管理
(对于如:删除客户端,所有连接的客户端的显示列表中,ETCC ..)
问题是:我怎么可以附加(或写)控制台,它可以在上面的功能调用。
此控制台必须是一个客户端?我应该运行这个控制台客户端作为sepearate线程?
我读了很多的教程,我couldn`t找到解决我的问题。
ServerTCP code
类ServerTCP
{
上市:
ServerTCP(升压:: ASIO :: io_service对象和放大器; A_ioService,无符号短A_uPortNumber = 13)
:m_tcpAcceptor(A_ioService,TCP ::端点(TCP :: V4(),A_uPortNumber)),m_ioService(A_ioService)
{
开始();
}
私人的: 无效的start()
{
ClientSessionPtr spClient(新的ClientSession(m_tcpAcceptor.io_service(),m_connectedClients)); m_tcpAcceptor.async_accept(spClient-> getSocket(),
提高::绑定(安培; ServerTCP :: handleAccept,为此,spClient,
提高:: ASIO ::占位符::错误)); }
无效handleAccept(ClientSessionPtr A_spNewClient,常量的boost ::系统::错误_ code和; A_nError)
{
如果(!A_nError)
{
A_spNewClient->启动();
开始();
}
} 提高:: ASIO :: io_service对象和放大器; m_ioService;
TCP ::受m_tcpAcceptor;
客户m_connectedClients;
};
主要功能:
尝试
{
提高:: ASIO :: io_service对象IOService的; ServerTCP服务器(IOService的);
ioService.run();
}
赶上(性病::例外急症)
{
的std :: CERR<< 例外:<< e.what()&所述;&下; \\ n;
}
您好萨姆。感谢您的回复。你这么善良,给我一个有些片code或一些链接的例子涉及到这个问题?
Propably,我不`吨正确理解......单线程服务器...
在控制台事实,我要管理服务器操作,我需要SMT象下面这样:
的main()COUT<< 选项:Q - 关闭服务器,S - 秀客;
而(1)
{
字符键= _getch();
开关(钥匙)
{
案例'Q':
closeServer();
打破
案件的:
showClients();
打破
}
}
问题是:我怎么可以附加(或
写)控制台,它可以在上面调用
功能。这个控制台有
是一个客户端?我应该运行此控制台
客户端作为sepearate线程?
块引用>您不需要一个单独的线程,使用
POSIX :: stream_descriptor
和<一个href=\"http://www.boost.org/doc/libs/1_46_0/doc/html/boost_asio/reference/posix__basic_stream_descriptor/assign/overload1.html\"相对=nofollow>分配STDIN_FILENO
它。使用async_read
和处理在读取处理的请求。的#include&LT;升压/ asio.hpp&GT;#包括LT&;升压/ bind.hpp&GT;
#包括LT&;升压/ enable_shared_from_this.hpp&GT;
#包括LT&;升压/ shared_ptr.hpp&GT;#包括LT&;&iostream的GT;使用空间boost :: ASIO;类输入:公众的boost :: enable_shared_from_this&LT;输入&GT;
{
上市:
TYPEDEF提高:: shared_ptr的&LT;输入&GT; PTR;上市:
静态无效创建(
io_service对象和放大器; io_service对象
)
{
PTR输入(
新的输入(io_service对象)
);
输入 - &GT;阅读();
}私人的:
明确的输入(
io_service对象和放大器; io_service对象
):
_Input(io_service对象)
{
_input.assign(STDIN_FILENO);
} 无效的read()
{
async_read(
_Input,
提高:: ASIO ::缓冲(放大器; _command,sizeof的(_command))
提高::绑定(
&安培;输入:: read_handler,
shared_from_this(),
占位符::错误
占位符:: bytes_transferred
)
);
} 无效read_handler(
常量的boost ::系统::错误_ code&放;错误,
为size_t bytes_transferred
)
{
如果(错误){
的std :: CERR&LT;&LT; 读取错误:&LT;&LT;提高::系统:: SYSTEM_ERROR(错误)。什么()&LT;&LT;的std :: ENDL;
返回;
} 如果(_command!='\\ n'){
性病::法院LT&;&LT; 命令:&LT;&LT; _command&LT;&LT;的std :: ENDL;
} 这 - &GT;阅读();
}私人的:
POSIX :: stream_descriptor _Input;
烧焦_command;
};INT
主要()
{
io_service对象io_service对象;
输入::创建(io_service对象);
io_service.run();
}I have to write asynchronous TCP Sever. TCP Server have to be managed by console (for eg: remove client, show list of all connected client, etcc..)
The problem is: How can I attach (or write) console, which can calls above functionalities. This console have to be a client? Should I run this console client as a sepearate thread?
I have read a lot of tutorials and I couldn`t find a solution to my problem.
ServerTCP code
class ServerTCP { public: ServerTCP(boost::asio::io_service& A_ioService, unsigned short A_uPortNumber = 13) : m_tcpAcceptor(A_ioService, tcp::endpoint(tcp::v4(), A_uPortNumber)), m_ioService (A_ioService) { start(); } private: void start() { ClientSessionPtr spClient(new ClientSession(m_tcpAcceptor.io_service(), m_connectedClients)); m_tcpAcceptor.async_accept(spClient->getSocket(), boost::bind(&ServerTCP::handleAccept, this, spClient, boost::asio::placeholders::error)); } void handleAccept(ClientSessionPtr A_spNewClient, const boost::system::error_code& A_nError) { if ( !A_nError ) { A_spNewClient->start(); start(); } } boost::asio::io_service& m_ioService; tcp::acceptor m_tcpAcceptor; Clients m_connectedClients; };
Main function:
try { boost::asio::io_service ioService; ServerTCP server(ioService); ioService.run(); } catch (std::exception& e) { std::cerr << "Exception: " << e.what() << "\n"; }
Hello Sam. Thanks for reply. Could you be so kind and show me a some piece of code or some links to examples involve with this problem ? Propably, I don`t understand correctly "... single threaded server ..."
In Fact in "console" where I want to manage server operations, I need smt like below:
main() cout << "Options: q - close server, s - show clients"; while(1) { char key = _getch(); switch( key ) { case 'q': closeServer(); break case 's': showClients(); break } }
解决方案The problem is: How can I attach (or write) console, which can calls above functionalities. This console have to be a client? Should I run this console client as a sepearate thread?
You don't need a separate thread, use a
posix::stream_descriptor
and assignSTDIN_FILENO
to it. Useasync_read
and handle the requests in the read handlers.#include <boost/asio.hpp> #include <boost/bind.hpp> #include <boost/enable_shared_from_this.hpp> #include <boost/shared_ptr.hpp> #include <iostream> using namespace boost::asio; class Input : public boost::enable_shared_from_this<Input> { public: typedef boost::shared_ptr<Input> Ptr; public: static void create( io_service& io_service ) { Ptr input( new Input( io_service ) ); input->read(); } private: explicit Input( io_service& io_service ) : _input( io_service ) { _input.assign( STDIN_FILENO ); } void read() { async_read( _input, boost::asio::buffer( &_command, sizeof(_command) ), boost::bind( &Input::read_handler, shared_from_this(), placeholders::error, placeholders::bytes_transferred ) ); } void read_handler( const boost::system::error_code& error, size_t bytes_transferred ) { if ( error ) { std::cerr << "read error: " << boost::system::system_error(error).what() << std::endl; return; } if ( _command != '\n' ) { std::cout << "command: " << _command << std::endl; } this->read(); } private: posix::stream_descriptor _input; char _command; }; int main() { io_service io_service; Input::create( io_service ); io_service.run(); }
这篇关于BOOST ASIO - 如何写控制台服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!