Advertisement
kolbka_

Untitled

Feb 24th, 2022
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include <algorithm>
  2. #include <boost/asio.hpp>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <string>
  6. #include <thread>
  7. #include <utility>
  8. #include "./cmake-build-debug/player.pb.h"
  9.  
  10. using boost::asio::ip::tcp;
  11.  
  12.  
  13. // NOLINTNEXTLINE
  14. int main(int argc, char *argv[]) {
  15.  
  16.  
  17.     boost::asio::io_context io_context;
  18.     tcp::acceptor acceptor(io_context, tcp::endpoint(tcp::v4(), 8000));
  19.     std::cout << "Listening at " << acceptor.local_endpoint() << std::endl;
  20.  
  21.     while (true) {
  22.         tcp::socket s = acceptor.accept();
  23.  
  24.         std::thread([s = std::move(s)]() mutable {
  25.             std::cout << "Connected client: " << s.remote_endpoint() << " --> " << s.local_endpoint() << std::endl;
  26.             tcp::iostream client(std::move(s));
  27.             std::string mes;
  28.             client << "Hello from C++ server!\n";
  29.  
  30.             while (client) {
  31.                 std::string buffer;
  32.                 PlayerAction tmp_;
  33.                 if (client >> buffer){
  34.                     std::cout << buffer;
  35.                 }
  36.                 getline(client, buffer);
  37.  
  38.                 tmp_.ParseFromString(buffer);
  39.                 std::string answer_from_server;
  40.                 std::cout << tmp_.key_pressed() << '\n';
  41.                 tmp_.SerializePartialToString(&answer_from_server);
  42.                 std::cout << "aboba\n";
  43.                 std::cout << answer_from_server << ' ' << answer_from_server.size();
  44. //                std::cout << (answer_from_server != "");
  45.                 client << "take it back: " << answer_from_server << std::endl;
  46. //              std::cout << "Received: " << msg << std::endl;
  47.  
  48.             }
  49. //                client >> buffer;
  50.  
  51.             std::cout << "Disconnected" << std::endl;
  52.         }).detach();
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement