#include <iostream>
#include <fstream>
#include <sstream>
#include <boost/asio.hpp>
using namespace std;
using boost::asio::ip::tcp;
const char* serviceList = "\n\t Services\n"
"\t**************************\n"
"\t[1] Get current time.\n"
"\t[2] Who's online.\n"
"\t[3] Get system info.\n"
"\t**************************\n\n"
"Please pick a service[1-3]: ";
ifstream fin;
void getResult(const string& cmdPrefix, const char* outputFile,
string& res) {
// cmd == "w > who"
string cmd(cmdPrefix + outputFile);
system(cmd.c_str());//将字符串解析为系统命令,然后执行
fin.open(outputFile);//存在打开文件,否则创建
if (fin) { ostringstream os; os << fin.rdbuf(); res = os.str();
}
if (fin.is_open()) { fin.close(); }
}
string getServiceContent(const int& select) {
string res;
switch (select) { //time(0)获取当前时间,ctime将时间转换成字
符串。 case 1: { time_t t = time(0); res = ctime(&t); break;
} case 2: getResult("w > ", "who", res); break; case 3: getResult("uname -a > ", "uname", res); break;
default: res = "Sorry, no such service.\n"; break; }
return res; }
int main() { try { boost::asio::io_service io_service; // #1
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4 //tcp::endpoint(端点)由ip地址,端口号,协议版本组成。
(), 8868)); // #2 for (;;) { tcp::socket socket(io_service); // #3 /不断创建socket对象,并且与acceptor进行关联
acceptor.accept(socket); // #4
// 1, send service list to client
boost::system::error_code ignored_error;
boost::asio::write(socket, boost::asio::buffer(serviceList), //buffer相当于显示器的一个缓冲,临时存储数据的一个容器
boost::asio::transfer_all(), ignored_error); //自我感觉read和write是一个固定套路
// 2, receive selection from client char selection[20];
size_t n = socket.read_some(boost::asio::buffer(selection),
ignored_error); // 3, send response
string response = getServiceContent(atoi(selection));
boost::asio::write(socket, boost::asio::buffer(response),
boost::asio::transfer_all(), ignored_error);
} // #6 } catch (std::exception& e) {
std::cerr << e.what() << std::endl; }
return 0; }
************************************************************************
/* * tcp_sync_client.cpp * * Created on: 2010-3-16 上午11:47:12 * Author: kwarph * Web: http://www.xuanyuan-soft.cn * Mail: kwarph@gmail.com */
#include <iostream>
#include <boost/asio.hpp>
using namespace std;
using boost::asio::ip::tcp;
int main(int argc, char* argv[]) {
try { // 1, 创建io_service对象
boost::asio::io_service io_service;
// 2, 创建resolver对象关联到io_service对象
tcp::resolver resolver(io_service);
// 3, 创建一个查询对象
tcp::resolver::query query("localhost", "8868");
// 4, 用resolver对象和查询对象获取可用服务器地址
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); //把用resolve解析query的即localhost首地址给endpoint_iterator tcp::resolver::iterator end;
//iterator就是endpoint的指针
// 5, 创建tcp::socket对象,关联到io_service
tcp::socket socket(io_service);
// 6, socket对象发起到服务器端的同步连接操作
boost::system::error_code error = boost::asio::error::host_not_found;
while (error && endpoint_iterator != end) { socket.close();
socket.connect(*endpoint_iterator++, error); }
if (error) // 如果没有一个地址能连接成功,则抛出异常
throw boost::system::system_error(error);
// 7, 一系列 同步read()和write()
char buf[512];
//1. receive service list from server
// size_t len = socket.read_some(boost::asio::buffer(buf), error); //
size_t len = socket.receive(boost::asio::buffer(buf), 0, error);
//2.confirm size of info with server//进行read操作时,先socket.read_some r然后才是asio::read. write则相反,需先asio::write,再socket.write_some,b并且还要socket.send
size_t len = boost::asio::read(socket, boost::asio::buffer(buf), boost::asio::transfer_at_least(1), error);//asio读写时的参数更多
buf[len] = '\0'; cout << buf;
string selection; cin >> selection;
//3. send selection to server //
boost::asio::write(socket, boost::asio::buffer(selection), //
boost::asio::transfer_all(), error);
// socket.write_some(boost::asio::buffer(selection,selection.size()), error);
socket.send(boost::asio::buffer(selection,selection.size()),0, error);
//4. receive response from server
len = socket.read_some(boost::asio::buffer(buf), error);
buf[len] = '\0'; cout << buf; } catch (std::exception& e) {
std::cerr << e.what() << std::endl; }
return 0; }