Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Client.cpp:
- //
- #define _CRT_SECURE_NO_WARNINGS
- #pragma warning(disable : 4996)
- #include "stdafx.h"
- #define _WIN32_WINNT 0x0501
- #include <stdio.h>
- #include <iostream>
- #include <string>
- #include <boost/date_time/posix_time/posix_time.hpp>
- //#include <boost/thread.hpp>
- #include <boost/bind.hpp>
- #include <boost/asio.hpp>
- #include <boost/shared_ptr.hpp>
- #include <boost/enable_shared_from_this.hpp>
- #include <windows.h>
- using namespace boost::asio;
- using namespace boost::posix_time;
- io_service service;
- UINT_PTR LINK_TIMER1 = 0;
- #define MEM_FN(x) boost::bind(&self_type::x, shared_from_this())
- #define MEM_FN1(x,y) boost::bind(&self_type::x, shared_from_this(),y)
- #define MEM_FN2(x,y,z) boost::bind(&self_type::x, shared_from_this(),y,z)
- //
- //Left trim
- //
- std::string trim_left(const std::string& str)
- {
- const std::string pattern = " fnrtv";
- return str.substr(str.find_first_not_of(pattern));
- }
- //
- //Right trim
- //
- std::string trim_right(const std::string& str)
- {
- const std::string pattern = " fnrtv";
- return str.substr(0, str.find_last_not_of(pattern) + 1);
- }
- //
- //Left and Right trim
- //
- std::string trim(const std::string& str)
- {
- return trim_left(trim_right(str));
- }
- std::string nAdd(std::string str)
- {
- if (str.find("n") == std::string::npos) {
- //отсутствует
- str += "n";
- }
- return str;
- }
- std::string nRem(std::string str)
- {
- if (str.find("n") != std::string::npos) {
- //есть
- //str += "n";
- str = trim(str);
- }
- return str;
- }
- class talk_to_svr : public boost::enable_shared_from_this<talk_to_svr>
- , boost::noncopyable {
- typedef talk_to_svr self_type;
- talk_to_svr(const std::string & message)
- : sock_(service), started_(true), message_(message) {}
- void start(ip::tcp::endpoint ep) {
- sock_.async_connect(ep, MEM_FN1(on_connect, _1));
- boost::system::error_code ec;
- sock_.set_option(boost::asio::socket_base::reuse_address(true), ec);
- //std::cout << ec.message() << std::endl;
- }
- public:
- typedef boost::system::error_code error_code;
- typedef boost::shared_ptr<talk_to_svr> ptr;
- std::string message_;
- static ptr start(ip::tcp::endpoint ep, const std::string & message) {
- ptr new_(new talk_to_svr(message));
- new_->start(ep);
- return new_;
- }
- void stop() {
- if (!started_) return;
- started_ = false;
- sock_.close();
- std::cout << "stop()" << std::endl;
- }
- bool sock_is_open()
- {
- return sock_.is_open();
- }
- bool started() { return started_; }
- void do_write(const std::string & msg)
- {
- if (!started()) return;
- message_ = nAdd(msg);
- std::cout << "Sending " << nRem(message_) << std::endl;
- std::copy(message_.begin(), message_.end(), write_buffer_);
- sock_.async_write_some(buffer(write_buffer_, message_.size()),
- MEM_FN2(on_write, _1, _2));
- }
- private:
- void on_connect(const error_code & err) {
- if (!err)
- {
- do_write(nAdd(message_));
- }
- else
- {
- stop();
- }
- }
- void on_read(const error_code & err, size_t bytes) {
- if (!err) {
- std::string copy(read_buffer_, bytes - 1);
- copy = nAdd(copy);
- bool check = copy == message_;
- std::cout << "server echoed our " << nRem(copy) << ": "
- << (check ? "OK" : "FAIL") << std::endl;
- }
- else
- {
- std::cout << "on_read err" << std::endl;
- }
- //stop(); //разрыв после получения ответа
- }
- void on_write(const error_code & err, size_t bytes) {
- do_read();
- }
- void do_read() {
- async_read(sock_, buffer(read_buffer_),
- MEM_FN2(read_complete, _1, _2), MEM_FN2(on_read, _1, _2));
- std::cout << "do_read()" << std::endl;
- }
- size_t read_complete(const boost::system::error_code & err, size_t bytes) {
- if (err) return 0;
- bool found = std::find(read_buffer_, read_buffer_ + bytes, 'n') < read_buffer_ + bytes;
- // we read one-by-one until we get to enter, no buffering
- return found ? 0 : 1;
- }
- private:
- ip::tcp::socket sock_;
- enum { max_msg = 1024 };
- char read_buffer_[max_msg];
- char write_buffer_[max_msg];
- bool started_;
- //std::string message_;
- };
- talk_to_svr::ptr talk_cli;
- int main(int argc, char* argv[]) {
- //setlocale(LC_ALL, "Russian");
- ip::tcp::endpoint ep(ip::address::from_string("127.0.0.1"), 8001);
- talk_cli = talk_to_svr::start(ep, "Hello message");
- service.run();
- std::string mes;
- do {
- std::cout << "enter message: ";
- std::cin >> mes;
- talk_cli->do_write(mes);
- } while (mes != "stop");
- talk_cli->stop();
- service.stop();
- service.reset();
- system("pause");
- }
- // Server.cpp:
- //
- #define _CRT_SECURE_NO_WARNINGS
- #pragma warning(disable : 4996)
- #include "stdafx.h"
- #define _WIN32_WINNT 0x0501
- #include <stdio.h>
- #include <iostream>
- #include <string>
- #include <boost/date_time/posix_time/posix_time.hpp>
- //#include <boost/thread.hpp>
- #include <boost/bind.hpp>
- #include <boost/asio.hpp>
- #include <boost/shared_ptr.hpp>
- #include <boost/enable_shared_from_this.hpp>
- #include <windows.h>
- using namespace boost::asio;
- using namespace boost::posix_time;
- io_service service;
- UINT_PTR LINK_TIMER1 = 0;
- #define MEM_FN(x) boost::bind(&self_type::x, shared_from_this())
- #define MEM_FN1(x,y) boost::bind(&self_type::x, shared_from_this(),y)
- #define MEM_FN2(x,y,z) boost::bind(&self_type::x, shared_from_this(),y,z)
- //
- //Left trim
- //
- std::string trim_left(const std::string& str)
- {
- const std::string pattern = " fnrtv";
- return str.substr(str.find_first_not_of(pattern));
- }
- //
- //Right trim
- //
- std::string trim_right(const std::string& str)
- {
- const std::string pattern = " fnrtv";
- return str.substr(0, str.find_last_not_of(pattern) + 1);
- }
- //
- //Left and Right trim
- //
- std::string trim(const std::string& str)
- {
- return trim_left(trim_right(str));
- }
- std::string nAdd(std::string str)
- {
- if (str.find("n") == std::string::npos) {
- //отсутствует
- str += "n";
- }
- return str;
- }
- std::string nRem(std::string str)
- {
- if (str.find("n") != std::string::npos) {
- //есть
- //str += "n";
- str = trim(str);
- }
- return str;
- }
- class talk_to_client : public boost::enable_shared_from_this<talk_to_client>, boost::noncopyable {
- typedef talk_to_client self_type;
- talk_to_client() : sock_(service), started_(false) {}
- public:
- typedef boost::system::error_code error_code;
- typedef boost::shared_ptr<talk_to_client> ptr;
- void start() {
- started_ = true;
- do_read();
- }
- static ptr new_() {
- ptr new_(new talk_to_client);
- return new_;
- }
- void stop() {
- if (!started_) return;
- started_ = false;
- sock_.close();
- }
- ip::tcp::socket & sock() { return sock_; }
- private:
- void on_read(const error_code & err, size_t bytes) {
- if (!err) {
- std::string msg(read_buffer_, bytes);
- // echo message back, and then stop
- do_write(nAdd(msg));
- std::cout << nRem(msg) << std::endl;
- }
- //stop(); //остановка после 1го ответа
- }
- void on_write(const error_code & err, size_t bytes) {
- if (err)
- {
- std::cout << "on_write error_code" << std::endl;
- }
- do_read();
- }
- void do_read() {
- async_read(sock_, buffer(read_buffer_),
- MEM_FN2(read_complete, _1, _2), MEM_FN2(on_read, _1, _2));
- }
- void do_write(const std::string & msg) {
- std::copy(msg.begin(), msg.end(), write_buffer_);
- sock_.async_write_some(buffer(write_buffer_, msg.size()),
- MEM_FN2(on_write, _1, _2));
- std::cout << "Sending back " << nRem(msg) << std::endl;
- }
- size_t read_complete(const boost::system::error_code & err, size_t bytes) {
- if (err) return 0;
- bool found = std::find(read_buffer_, read_buffer_ + bytes, 'n') < read_buffer_ + bytes;
- // we read one-by-one until we get to enter, no buffering
- return found ? 0 : 1;
- }
- private:
- ip::tcp::socket sock_;
- enum { max_msg = 1024 };
- char read_buffer_[max_msg];
- char write_buffer_[max_msg];
- bool started_;
- };
- ip::tcp::acceptor acceptor(service, ip::tcp::endpoint(ip::tcp::v4(), 8001));
- void handle_accept(talk_to_client::ptr client, const boost::system::error_code & err) {
- client->start();
- talk_to_client::ptr new_client = talk_to_client::new_();
- acceptor.async_accept(new_client->sock(), boost::bind(handle_accept, new_client, _1));
- }
- talk_to_client::ptr talk_ser;
- int main(int argc, char* argv[]) {
- //setlocale(LC_ALL, "Russian");
- //
- talk_ser = talk_to_client::new_();
- acceptor.async_accept(talk_ser->sock(), boost::bind(handle_accept, talk_ser, _1));
- service.run();
- system("pause");
- }
Add Comment
Please, Sign In to add comment