Boost库客户端

编程入门 行业动态 更新时间:2024-10-25 12:28:31
Boost库客户端 - 服务器应用程序(Boost library client-server application)

我从boost文档网站构建了简单的boost应用程序,但仍然不知道如何使用它。

1- Server application: #include <ctime> #include <iostream> #include <string> #include <boost/asio.hpp> using boost::asio::ip::tcp; std::string make_daytime_string() { using namespace std; // For time_t, time and ctime; time_t now = time(0); return ctime(&now); } int main() { try { boost::asio::io_service io_service; tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 13)); for (;;) { tcp::socket socket(io_service); acceptor.accept(socket); std::string message = make_daytime_string(); boost::system::error_code ignored_error; boost::asio::write(socket, boost::asio::buffer(message), ignored_error); } } catch (std::exception& e) { std::cerr << e.what() << std::endl; } return 0; }

2-客户申请:

#include<iostream> #include<exception> #include "boost\array.hpp" #include "boost\asio.hpp" using namespace std; using namespace boost; using boost::asio::ip::tcp; int main(int argc, char *argv[]) { try { if (argc != 2) { cerr << "usage: client <host>" << endl; return 1; } asio::io_service io_service; tcp::resolver resolver(io_service); tcp::resolver::query query(argv[1], "daytime"); tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); tcp::socket socket(io_service); asio::connect(socket, endpoint_iterator); for (;;) { boost::array<char, 128> buf; system::error_code error_code; size_t len = socket.read_some(asio::buffer(buf), error_code); if (error_code == asio::error::eof) break; //Connection closed. else throw system::system_error(error_code); cout.write(buf.data(), len); } } catch (std::exception& e) { cerr << e.what() << endl; } while (true) { } return 0; }

那么,接下来呢?

我exe然后客户端,但没有看到任何更多的Flash控制台应用程序。

(注意:这两个应用程序编译都很好,配置没有问题。

I built simple boost application from boost documentation website but still don't know how to use it yet.

1- Server application: #include <ctime> #include <iostream> #include <string> #include <boost/asio.hpp> using boost::asio::ip::tcp; std::string make_daytime_string() { using namespace std; // For time_t, time and ctime; time_t now = time(0); return ctime(&now); } int main() { try { boost::asio::io_service io_service; tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 13)); for (;;) { tcp::socket socket(io_service); acceptor.accept(socket); std::string message = make_daytime_string(); boost::system::error_code ignored_error; boost::asio::write(socket, boost::asio::buffer(message), ignored_error); } } catch (std::exception& e) { std::cerr << e.what() << std::endl; } return 0; }

2- client application:

#include<iostream> #include<exception> #include "boost\array.hpp" #include "boost\asio.hpp" using namespace std; using namespace boost; using boost::asio::ip::tcp; int main(int argc, char *argv[]) { try { if (argc != 2) { cerr << "usage: client <host>" << endl; return 1; } asio::io_service io_service; tcp::resolver resolver(io_service); tcp::resolver::query query(argv[1], "daytime"); tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); tcp::socket socket(io_service); asio::connect(socket, endpoint_iterator); for (;;) { boost::array<char, 128> buf; system::error_code error_code; size_t len = socket.read_some(asio::buffer(buf), error_code); if (error_code == asio::error::eof) break; //Connection closed. else throw system::system_error(error_code); cout.write(buf.data(), len); } } catch (std::exception& e) { cerr << e.what() << endl; } while (true) { } return 0; }

so, what next?

I exe then the client but dont see any more that flash console application.

(note: both application are compiled fine and no problem with the configuration.

最满意答案

for (;;) { ... if (error_code == asio::error::eof) break; //Connection closed. else throw system::system_error(error_code); //code after this line will never be executed cout.write(buf.data(), len); }

您的客户端不会在控制台中写入任何数据 - 执行循环将会因抛出异常或break命令而中断

for (;;) { ... if (error_code == asio::error::eof) break; //Connection closed. else throw system::system_error(error_code); //code after this line will never be executed cout.write(buf.data(), len); }

Your client don't write any data in console - execution of loop will be breaked over throwing exception or break command

更多推荐

本文发布于:2023-08-07 06:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462595.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   Boost

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!