Advertisement
Guest User

Untitled

a guest
Aug 20th, 2016
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. #include <ctime>
  2. #include <iostream>
  3. #include <string>
  4. #include <boost/bind.hpp>
  5. #include <boost/shared_ptr.hpp>
  6. #include <boost/enable_shared_from_this.hpp>
  7. #include <boost/asio.hpp>
  8. #include <boost/array.hpp>
  9.  
  10. using boost::asio::ip::tcp;
  11.  
  12. std::string make_daytime_string()
  13. {
  14. using namespace std; // For time_t, time and ctime;
  15. time_t now = time(0);
  16. return ctime(&now);
  17. }
  18.  
  19.  
  20. class tcp_connection: public boost::enable_shared_from_this<tcp_connection>
  21. {
  22. public:
  23. typedef boost::shared_ptr<tcp_connection> pointer;
  24.  
  25. static pointer create(boost::asio::io_service& io_service)
  26. {
  27. return pointer(new tcp_connection(io_service));
  28. }
  29.  
  30. tcp::socket& socket()
  31. {
  32. return socket_;
  33. }
  34.  
  35. // Call boost::asio::async_write() to serve the data to the client.
  36. // We are using boost::asio::async_write(),
  37. // rather than ip::tcp::socket::async_write_some(),
  38. // to ensure that the entire block of data is sent.
  39.  
  40. void start()
  41. {
  42. // The data to be sent is stored in the class member m_message
  43. // as we need to keep the data valid
  44. // until the asynchronous operation is complete.
  45.  
  46. m_message = make_daytime_string();
  47.  
  48. // When initiating the asynchronous operation,
  49. // and if using boost::bind(),
  50. // we must specify only the arguments
  51. // that match the handler's parameter list.
  52. // In this code, both of the argument placeholders
  53. // (boost::asio::placeholders::error
  54. // and boost::asio::placeholders::bytes_transferred)
  55. // could potentially have been removed,
  56. // since they are not being used in handle_write().
  57.  
  58. std::cout << socket_.remote_endpoint().address().to_string() << ":" << socket_.remote_endpoint().port() << " connected!" << std::endl;
  59.  
  60. boost::asio::async_write(socket_, boost::asio::buffer(m_message),
  61. boost::bind(&tcp_connection::handle_write, shared_from_this()));
  62.  
  63. boost::asio::async_read(socket_, boost::asio::buffer(_buffer), boost::bind(&tcp_connection::handle_receive, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
  64. }
  65.  
  66.  
  67. private:
  68. tcp_connection(boost::asio::io_service& io_service)
  69. : socket_(io_service)
  70. {
  71. }
  72. // handle_write() is responsible for any further actions
  73. // for this client connection.
  74.  
  75. void handle_write() // call back.. when it finishes sending, we come here
  76. {
  77. std::cout << "Client has received the messaged. " << std::endl;
  78. }
  79.  
  80. void handle_receive(const boost::system::error_code& ErrorCode, std::size_t bytes_transferred)
  81. {
  82. std::cout << "You received the following message from the server:" << std::endl;
  83. std::cout.write(_buffer.data(), bytes_transferred);
  84.  
  85. }
  86.  
  87. tcp::socket socket_;
  88. std::string m_message;
  89. boost::array<char, 126> _buffer;
  90. };
  91.  
  92. class tcp_server
  93. {
  94. public:
  95. tcp_server(boost::asio::io_service& io_service) : acceptor_(io_service, tcp::endpoint(tcp::v4(), 7171))
  96. {
  97. // start_accept() creates a socket and
  98. // initiates an asynchronous accept operation
  99. // to wait for a new connection.
  100. start_accept();
  101. }
  102.  
  103. private:
  104. void start_accept()
  105. {
  106. // creates a socket
  107. tcp_connection::pointer new_connection = tcp_connection::create(acceptor_.get_io_service());
  108.  
  109. // initiates an asynchronous accept operation
  110. // to wait for a new connection.
  111. acceptor_.async_accept(new_connection->socket(),
  112. boost::bind(&tcp_server::handle_accept, this, new_connection,
  113. boost::asio::placeholders::error));
  114. }
  115.  
  116. // handle_accept() is called when the asynchronous accept operation
  117. // initiated by start_accept() finishes. It services the client request
  118. void handle_accept(tcp_connection::pointer new_connection,
  119. const boost::system::error_code& error)
  120. {
  121. if (!error)
  122. {
  123. new_connection->start();
  124. }
  125.  
  126. // Call start_accept() to initiate the next accept operation.
  127. start_accept();
  128. }
  129.  
  130. tcp::acceptor acceptor_;
  131. };
  132.  
  133. int main()
  134. {
  135. std::cout << "Server is online!" << std::endl;
  136. try
  137. {
  138. boost::asio::io_service io_service;
  139.  
  140. tcp_server server(io_service);
  141.  
  142. io_service.run();
  143.  
  144. }
  145. catch (std::exception& e)
  146. {
  147. std::cerr << e.what() << std::endl;
  148. }
  149.  
  150. return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement