0
私はブーストドキュメントのWebサイトから簡単なブーストアプリケーションを構築しましたが、まだそれを使用する方法はまだ分かりません。ブーストライブラリクライアント/サーバーアプリケーション
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していますが、フラッシュコンソールアプリケーションはもう表示されません。
は(注:両方のアプリケーションは、罰金や構成で問題なくコンパイルされ
コンソール出力を見たい場合は、 'while'ループの最後に' std :: cin.get() 'を追加してください。 – Wum
@Wum Huhさん、何ですか?私はむしろ2つの端末を開き、 –
このサーバは、接続が成功した後に現在の日付と時刻をクライアントに送信して終了します.c lient _doesはメッセージを表示せず、終了します。 (@ DmitryBakhtiyarovの答えを参照)それ以上はありません。 – user4407569