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 "boostarray.hpp"
#include "boostasio.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控制台应用程序。
(注意:两个应用程序都编译得很好,配置没有问题。
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);
}
你的客户端不写任何数据在控制台-执行循环将被中断抛出异常或中断命令
相关文章:
- 使用 QT 开发服务器应用程序是个好主意吗?(QT5)
- 在POCO服务器应用程序中预防目录遍历攻击
- 仅使用UDP托管小型服务器应用程序的缺点是什么
- 客户端服务器应用程序的频道
- 在服务器应用程序的情况下,是截断线被视为良好,中性或设计差的线程
- 使用WinSocket客户端/服务器应用程序重复使用插座
- 无法在 C++ 服务器应用程序上提交 iphlpapi.lib
- 执行析构函数时,服务器应用程序总是崩溃
- 在多线程c++服务器应用程序中处理非常量全局配置
- 在 CORBA 客户端/服务器应用程序中将无符号的长整型(从 C++)断言为长整型 (Java)
- CORBA客户端/服务器应用程序,从服务器获取信息(空值)
- 服务器应用程序的C++与Java
- 如何在C++服务器应用程序和django web应用程序之间进行通信
- 在Javascript服务器应用程序之后获取HTML
- 服务器应用程序看不到具有相对路径的文件
- 如何在没有源代码的tcp服务器应用程序中查找错误
- 如何在 ATL COM+ 进程外服务器应用程序中标识调用方的进程?
- 如何从 Http 请求处理程序正确终止 POCO 服务器应用程序
- 服务器应用程序.始终打开的插座
- 如何在Qt中编写客户端-服务器应用程序并实现简单的协议