Guest User

Untitled

a guest
May 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.91 KB | None | 0 0
  1. // Client.cpp:
  2. //
  3.  
  4. #define _CRT_SECURE_NO_WARNINGS
  5. #pragma warning(disable : 4996)
  6.  
  7. #include "stdafx.h"
  8. #define _WIN32_WINNT 0x0501
  9. #include <stdio.h>
  10. #include <iostream>
  11. #include <string>
  12. #include <boost/date_time/posix_time/posix_time.hpp>
  13. //#include <boost/thread.hpp>
  14. #include <boost/bind.hpp>
  15. #include <boost/asio.hpp>
  16. #include <boost/shared_ptr.hpp>
  17. #include <boost/enable_shared_from_this.hpp>
  18. #include <windows.h>
  19. using namespace boost::asio;
  20. using namespace boost::posix_time;
  21. io_service service;
  22. UINT_PTR LINK_TIMER1 = 0;
  23.  
  24.  
  25. #define MEM_FN(x) boost::bind(&self_type::x, shared_from_this())
  26. #define MEM_FN1(x,y) boost::bind(&self_type::x, shared_from_this(),y)
  27. #define MEM_FN2(x,y,z) boost::bind(&self_type::x, shared_from_this(),y,z)
  28.  
  29. //
  30. //Left trim
  31. //
  32. std::string trim_left(const std::string& str)
  33. {
  34. const std::string pattern = " fnrtv";
  35. return str.substr(str.find_first_not_of(pattern));
  36. }
  37.  
  38. //
  39. //Right trim
  40. //
  41. std::string trim_right(const std::string& str)
  42. {
  43. const std::string pattern = " fnrtv";
  44. return str.substr(0, str.find_last_not_of(pattern) + 1);
  45. }
  46.  
  47. //
  48. //Left and Right trim
  49. //
  50. std::string trim(const std::string& str)
  51. {
  52. return trim_left(trim_right(str));
  53. }
  54.  
  55. std::string nAdd(std::string str)
  56. {
  57. if (str.find("n") == std::string::npos) {
  58. //отсутствует
  59. str += "n";
  60. }
  61. return str;
  62. }
  63.  
  64. std::string nRem(std::string str)
  65. {
  66. if (str.find("n") != std::string::npos) {
  67. //есть
  68. //str += "n";
  69. str = trim(str);
  70. }
  71. return str;
  72. }
  73.  
  74. class talk_to_svr : public boost::enable_shared_from_this<talk_to_svr>
  75. , boost::noncopyable {
  76. typedef talk_to_svr self_type;
  77. talk_to_svr(const std::string & message)
  78. : sock_(service), started_(true), message_(message) {}
  79. void start(ip::tcp::endpoint ep) {
  80.  
  81. sock_.async_connect(ep, MEM_FN1(on_connect, _1));
  82. boost::system::error_code ec;
  83. sock_.set_option(boost::asio::socket_base::reuse_address(true), ec);
  84. //std::cout << ec.message() << std::endl;
  85. }
  86. public:
  87. typedef boost::system::error_code error_code;
  88. typedef boost::shared_ptr<talk_to_svr> ptr;
  89. std::string message_;
  90.  
  91. static ptr start(ip::tcp::endpoint ep, const std::string & message) {
  92. ptr new_(new talk_to_svr(message));
  93. new_->start(ep);
  94. return new_;
  95. }
  96. void stop() {
  97. if (!started_) return;
  98. started_ = false;
  99. sock_.close();
  100. std::cout << "stop()" << std::endl;
  101. }
  102. bool sock_is_open()
  103. {
  104. return sock_.is_open();
  105. }
  106. bool started() { return started_; }
  107.  
  108. void do_write(const std::string & msg)
  109. {
  110. if (!started()) return;
  111. message_ = nAdd(msg);
  112. std::cout << "Sending " << nRem(message_) << std::endl;
  113. std::copy(message_.begin(), message_.end(), write_buffer_);
  114. sock_.async_write_some(buffer(write_buffer_, message_.size()),
  115. MEM_FN2(on_write, _1, _2));
  116. }
  117. private:
  118. void on_connect(const error_code & err) {
  119. if (!err)
  120. {
  121. do_write(nAdd(message_));
  122. }
  123. else
  124. {
  125. stop();
  126. }
  127. }
  128. void on_read(const error_code & err, size_t bytes) {
  129. if (!err) {
  130. std::string copy(read_buffer_, bytes - 1);
  131. copy = nAdd(copy);
  132. bool check = copy == message_;
  133. std::cout << "server echoed our " << nRem(copy) << ": "
  134. << (check ? "OK" : "FAIL") << std::endl;
  135. }
  136. else
  137. {
  138. std::cout << "on_read err" << std::endl;
  139. }
  140. //stop(); //разрыв после получения ответа
  141. }
  142.  
  143. void on_write(const error_code & err, size_t bytes) {
  144. do_read();
  145. }
  146. void do_read() {
  147. async_read(sock_, buffer(read_buffer_),
  148. MEM_FN2(read_complete, _1, _2), MEM_FN2(on_read, _1, _2));
  149. std::cout << "do_read()" << std::endl;
  150. }
  151.  
  152. size_t read_complete(const boost::system::error_code & err, size_t bytes) {
  153. if (err) return 0;
  154. bool found = std::find(read_buffer_, read_buffer_ + bytes, 'n') < read_buffer_ + bytes;
  155. // we read one-by-one until we get to enter, no buffering
  156. return found ? 0 : 1;
  157. }
  158.  
  159. private:
  160. ip::tcp::socket sock_;
  161. enum { max_msg = 1024 };
  162. char read_buffer_[max_msg];
  163. char write_buffer_[max_msg];
  164. bool started_;
  165. //std::string message_;
  166. };
  167.  
  168. talk_to_svr::ptr talk_cli;
  169.  
  170. int main(int argc, char* argv[]) {
  171. //setlocale(LC_ALL, "Russian");
  172.  
  173. ip::tcp::endpoint ep(ip::address::from_string("127.0.0.1"), 8001);
  174. talk_cli = talk_to_svr::start(ep, "Hello message");
  175. service.run();
  176.  
  177. std::string mes;
  178. do {
  179. std::cout << "enter message: ";
  180. std::cin >> mes;
  181. talk_cli->do_write(mes);
  182. } while (mes != "stop");
  183.  
  184. talk_cli->stop();
  185. service.stop();
  186. service.reset();
  187. system("pause");
  188. }
  189.  
  190. // Server.cpp:
  191. //
  192.  
  193. #define _CRT_SECURE_NO_WARNINGS
  194. #pragma warning(disable : 4996)
  195.  
  196. #include "stdafx.h"
  197. #define _WIN32_WINNT 0x0501
  198. #include <stdio.h>
  199. #include <iostream>
  200. #include <string>
  201. #include <boost/date_time/posix_time/posix_time.hpp>
  202. //#include <boost/thread.hpp>
  203. #include <boost/bind.hpp>
  204. #include <boost/asio.hpp>
  205. #include <boost/shared_ptr.hpp>
  206. #include <boost/enable_shared_from_this.hpp>
  207. #include <windows.h>
  208. using namespace boost::asio;
  209. using namespace boost::posix_time;
  210. io_service service;
  211. UINT_PTR LINK_TIMER1 = 0;
  212.  
  213.  
  214. #define MEM_FN(x) boost::bind(&self_type::x, shared_from_this())
  215. #define MEM_FN1(x,y) boost::bind(&self_type::x, shared_from_this(),y)
  216. #define MEM_FN2(x,y,z) boost::bind(&self_type::x, shared_from_this(),y,z)
  217.  
  218. //
  219. //Left trim
  220. //
  221. std::string trim_left(const std::string& str)
  222. {
  223. const std::string pattern = " fnrtv";
  224. return str.substr(str.find_first_not_of(pattern));
  225. }
  226.  
  227. //
  228. //Right trim
  229. //
  230. std::string trim_right(const std::string& str)
  231. {
  232. const std::string pattern = " fnrtv";
  233. return str.substr(0, str.find_last_not_of(pattern) + 1);
  234. }
  235.  
  236. //
  237. //Left and Right trim
  238. //
  239. std::string trim(const std::string& str)
  240. {
  241. return trim_left(trim_right(str));
  242. }
  243.  
  244. std::string nAdd(std::string str)
  245. {
  246. if (str.find("n") == std::string::npos) {
  247. //отсутствует
  248. str += "n";
  249. }
  250. return str;
  251. }
  252.  
  253. std::string nRem(std::string str)
  254. {
  255. if (str.find("n") != std::string::npos) {
  256. //есть
  257. //str += "n";
  258. str = trim(str);
  259. }
  260. return str;
  261. }
  262.  
  263. class talk_to_client : public boost::enable_shared_from_this<talk_to_client>, boost::noncopyable {
  264. typedef talk_to_client self_type;
  265. talk_to_client() : sock_(service), started_(false) {}
  266. public:
  267. typedef boost::system::error_code error_code;
  268. typedef boost::shared_ptr<talk_to_client> ptr;
  269.  
  270. void start() {
  271. started_ = true;
  272. do_read();
  273. }
  274. static ptr new_() {
  275. ptr new_(new talk_to_client);
  276. return new_;
  277. }
  278. void stop() {
  279. if (!started_) return;
  280. started_ = false;
  281. sock_.close();
  282. }
  283. ip::tcp::socket & sock() { return sock_; }
  284. private:
  285. void on_read(const error_code & err, size_t bytes) {
  286. if (!err) {
  287. std::string msg(read_buffer_, bytes);
  288. // echo message back, and then stop
  289. do_write(nAdd(msg));
  290. std::cout << nRem(msg) << std::endl;
  291. }
  292. //stop(); //остановка после 1го ответа
  293. }
  294.  
  295. void on_write(const error_code & err, size_t bytes) {
  296. if (err)
  297. {
  298. std::cout << "on_write error_code" << std::endl;
  299. }
  300. do_read();
  301. }
  302. void do_read() {
  303. async_read(sock_, buffer(read_buffer_),
  304. MEM_FN2(read_complete, _1, _2), MEM_FN2(on_read, _1, _2));
  305. }
  306. void do_write(const std::string & msg) {
  307. std::copy(msg.begin(), msg.end(), write_buffer_);
  308. sock_.async_write_some(buffer(write_buffer_, msg.size()),
  309. MEM_FN2(on_write, _1, _2));
  310. std::cout << "Sending back " << nRem(msg) << std::endl;
  311. }
  312. size_t read_complete(const boost::system::error_code & err, size_t bytes) {
  313. if (err) return 0;
  314. bool found = std::find(read_buffer_, read_buffer_ + bytes, 'n') < read_buffer_ + bytes;
  315. // we read one-by-one until we get to enter, no buffering
  316. return found ? 0 : 1;
  317. }
  318. private:
  319. ip::tcp::socket sock_;
  320. enum { max_msg = 1024 };
  321. char read_buffer_[max_msg];
  322. char write_buffer_[max_msg];
  323. bool started_;
  324. };
  325.  
  326. ip::tcp::acceptor acceptor(service, ip::tcp::endpoint(ip::tcp::v4(), 8001));
  327.  
  328. void handle_accept(talk_to_client::ptr client, const boost::system::error_code & err) {
  329. client->start();
  330. talk_to_client::ptr new_client = talk_to_client::new_();
  331. acceptor.async_accept(new_client->sock(), boost::bind(handle_accept, new_client, _1));
  332. }
  333.  
  334. talk_to_client::ptr talk_ser;
  335.  
  336.  
  337. int main(int argc, char* argv[]) {
  338. //setlocale(LC_ALL, "Russian");
  339. //
  340. talk_ser = talk_to_client::new_();
  341. acceptor.async_accept(talk_ser->sock(), boost::bind(handle_accept, talk_ser, _1));
  342. service.run();
  343.  
  344. system("pause");
  345. }
Add Comment
Please, Sign In to add comment