2014年01月15日 星期三 16:21
Poco的Net模块提供了丰富的网络相关支持,使用也非常方便。
如下代码演示和如何使用TCPServer框架构建高性能多线程网络应用服务器。
#include <iostream> #include <string> #include <Poco/Net/ServerSocket.h> #include <Poco/Net/TCPServer.h> #include <Poco/Net/TCPServerConnection.h> #include <Poco/Net/TCPServerConnectionFactory.h> #include <Poco/Net/DialogSocket.h> #include <Poco/Util/ServerApplication.h> using namespace std; using namespace Poco; using namespace Poco::Net; using namespace Poco::Util; class DialogTCPServerConnection: public TCPServerConnection { public: DialogTCPServerConnection(const StreamSocket& socket): TCPServerConnection(socket),_ds(socket) { } void run() { while(_ds.receiveMessage(_message)) { _ds.sendMessage(_message); } } private: string _message; DialogSocket _ds; }; class DialogServer: public ServerApplication { public: int main(const std::vector<std::string>& args) { ServerSocket svs(12345); TCPServer srv(new TCPServerConnectionFactoryImpl<DialogTCPServerConnection>(),svs); srv.start(); waitForTerminationRequest(); srv.stop(); } }; int main(int argc,char **argv) { DialogServer dsrv; return dsrv.run(argc,argv); }
参考编译指令:
g++ -std=c++11 -o server server.cpp -l PocoNet -l PocoFoundation -l PocoUtil
参考资料:
/usr/include/Poco/Net/TCPServer.h
/usr/include/Poco/Net/TCPServerConnection.h
/usr/include/Poco/Net/TCPServerConnectionFactory.h
/usr/include/Poco/Net/DialogSocket.h
/usr/include/Poco/Util/ServerApplication.h
Zeuux © 2024
京ICP备05028076号